标签: retrofit2

Retrofit2 rxjava 2 - 出现错误时如何访问响应正文

我将 Retrofit 与 Rxjava 一起使用来向服务器发出请求。
我的服务器返回定义的 json 格式,其中包括数据和定义的消息。
服务器返回http响应。如果服务器返回成功代码(200)就可以了。
但我希望,如果服务器返回其他代码,我将管理该响应的正文。
例如:
服务器返回401,我想读取响应正文以显示服务器的消息。
但是当服务器其他代码时,改造调用 onError 方法,我无法使用响应正文。
如何解决这个问题?
这是我的方法

'''

private void login(String username , String password){
    view.setLoading();
    source.loginUser(username, password)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<Response<LoginResult>>() {
                @Override
                public void onSubscribe(Disposable d) {
                    disposable.add(d);
                }

                @Override
                public void onSuccess(Response<LoginResult> loginResult) {

                    if (loginResult.isSuccessful()){


                        }
                        else
         new AlertConfiguration(view.getViewActivity()).showMessage(loginResult.body().getMessage());

                    }



                @Override
                public void onError(Throwable e) {

           if there is a problem          
                }
            });
Run Code Online (Sandbox Code Playgroud)

'''
这是我改造的接口方法

@POST("...")
Single<Response<LoginResult>> loginUser(@Query("username") String username, @Query("password") String password);
Run Code Online (Sandbox Code Playgroud)

android observable android-studio retrofit2 rx-java2

1
推荐指数
1
解决办法
2208
查看次数

::class.java 的含义是什么

我正在尝试通过 Retrofit2 访问 Web api,所以我为此创建了一个构建器,其中我不明白这条线在做什么,我如何能够在其中传递我的接口

 val retrofitBuilder = Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build()

                //meaning of this line
                **.create(ApiInterface::class.java)**
Run Code Online (Sandbox Code Playgroud)

这是界面

interface ApiInterface {

    @GET("users")
    fun getData():Call<List<FakeJsonDataItemItem>>

}
Run Code Online (Sandbox Code Playgroud)

我在里面传递什么?

java android kotlin retrofit2

1
推荐指数
1
解决办法
1598
查看次数

我有空字段,但改造不发送到后端

我有这样的课程:

class MyData {

    @SerializedName("name")
    var name: String? = null

    @SerializedName("surName")
    var surName: String? = null
    
}
Run Code Online (Sandbox Code Playgroud)

我的服务调用是这样的。

interface MyServiceCall {
    
    @PUT("/My/endPoint")
    fun updateInfo(@Body info: MyData): Flowable<Response<ResponseBody>>

}
Run Code Online (Sandbox Code Playgroud)

但是当我只想发送姓名空时。null 不会转到后端,然后返回 400 - 错误请求。

所以我的问题是即使字段为空,如何将数据发送到后端?

android kotlin retrofit2

1
推荐指数
1
解决办法
3115
查看次数

Kotlin runBlocking 总是阻塞主线程?

关于runBlokcing的官方文档

\n
\n

运行新的协程并阻塞当前线程,直到\n完成。不应在协程中使用此函数。它旨在将常规阻塞代码桥接到以挂起方式编写的库,以在主函数和测试中使用。

\n
\n

我认为这意味着runBlocking仅阻止当前线程。

\n

Kotlin Coroutines Deep Dive所写的Marcin Moska\xc5\x82a
\n书上说

\n
\n

“使用调度程序,我们可以使 runBlocking 在不同的线程上运行。但是,启动此构建器的线程仍然会被阻塞,直到协程完成为止。”

\n
\n

我理解,即使我将当前线程更改为工作线程(或其他)线程withContext,并在工作线程中运行“runBlocking”,runBlocking仍然会阻止包括主线程在内的所有线程。

\n

因此,我想知道的是runBlocking无论我使用什么方式总是阻塞主线程?

\n

android kotlin retrofit2 kotlin-coroutines

1
推荐指数
2
解决办法
917
查看次数

使用带有POJO类的Retrofit SimpleXml-Converter解析以下XML

这是我的xml结构

<downloads>
<item>98cfa929ee93149e245aabf5e4377058</item>
<item>498b513aa646d6ef1c407cbeabf6bd20</item>
<item>13815d2c0dd53a251bb53717b9f43b64</item>
<item>7a0615eb601264bbae0ad510a31fd61d</item>
<item>3157910f72705e60d0d4ba26c3fb0e84</item>
<item>29e037200d14e21b4ccdfbfaaf1621e8</item>
<item>5f5da6505eaa203d65d1a6203dd0e742</item>
<item>98dcbf2a73016cc9e72c3cbc101de151</item>
<item>7b21d3f11bf6b12e4c4abca7004ad80d</item>
<item>116049d5a4e53240b76666c6826fc6d6</item>
<item>c7ec466398f3d8cee56147c696760076</item>
<item>66ce3335de344b32986d2b77ff992ae2</item>
</downloads>
Run Code Online (Sandbox Code Playgroud)

和细节xml

<download>
<item>
<key>9b94e79fecb1f055f279d95e800867f9</key>
<value>
A Jedi uses the Force for knowledge and defense, never for attack.
</value>
</item>
</download>
Run Code Online (Sandbox Code Playgroud)

这是我想从服务器检索并将上面的xml转换为它.

 public class DownloadPayloadCollection {
        // Class log identifier
        public final static String LOG_TAG = DownloadPayloadCollection.class.getSimpleName();
        @Root(name = "downloads")
        @ElementList(inline=true)
        private List<DownloadPayload> result;


        public DownloadPayloadCollection() {
        }

        public List<DownloadPayload> getResult() {
            Log.d(LOG_TAG, "CALLED FROM getResult() " + result.toString());
            return result;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是应该表示item元素的类.

@Root(name = …
Run Code Online (Sandbox Code Playgroud)

xml android xml-parsing retrofit2

0
推荐指数
1
解决办法
2870
查看次数

使用Retrofit 2将根单个元素添加到xml

我在xml中有一个服务器响应,该响应格式不正确,也没有根元素:

<option value="stationAValue">stationADescription</option>
<option value="stationBValue">stationBDescription</option>
Run Code Online (Sandbox Code Playgroud)

我试图这样使用SimpleXmlConverterFactory

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(Params.BASE_URL)
    .client(okHttpClient)
    .addConverterFactory(SimpleXmlConverterFactory.create())
    .build();
Run Code Online (Sandbox Code Playgroud)

这是我的类,代表一行:

public class Station {
    @Element(name = "option")
    private String mName;

   @Attribute(required = false)
   private String value;
}
Run Code Online (Sandbox Code Playgroud)

但是,当然,如果没有根元素就无法解析它,是否有一种方法可以在SimpleXmlConverterFactory尝试解析响应之前添加根元素?

还是另一种解决方案?

xml android retrofit2 simple-xml-converter

0
推荐指数
1
解决办法
1005
查看次数

如何正确设置改造参数

我正在尝试使用Retrofit 2创建Weather应用程序,现在我很难正确设置呼叫.

这是正在运行的URL:

http://api.openweathermap.org/data/2.5/weather?q=London&APPID=MY_API_KEY
Run Code Online (Sandbox Code Playgroud)

所以,我有我的API密钥和BASE URL是:http://api.openweathermap.org ..这是我的Retrofit服务中的方法:

    @GET("/data/2.5/weather?q={city}/&APPID={api}")
    Observable<WeatherResponse> getWeather(@Path("city") String city, @Path("api") String api);
Run Code Online (Sandbox Code Playgroud)

而我得到的错误是:

java.lang.IllegalArgumentException:URL查询字符串"q = {city} /&APPID = {api}"必须没有替换块.对于动态查询参数,请使用@Query.

所以我试着这样:

@GET("/data/2.5/weather?{city}/&APPID={api}")
Observable<WeatherResponse> getWeather(@Query("city") String city, @Path("api") String api);
Run Code Online (Sandbox Code Playgroud)

我得到同样的错误......任何人都知道这里的交易是什么,我的网址有什么问题?

android android-developer-api retrofit2 rx-java2

0
推荐指数
1
解决办法
1677
查看次数

当我请求相同的url时,如何在retrofix2.0(rxandroid)中取消请求

我想在用户点击按钮时调用请求.但是当第一次请求没有响应时,用户想要更改参数并使用相同的URL调用新请求.

问题 如何取消第一个请求并使用相同的URL调用新请求.

 ServiceFactory
            .createService()
            .getSomething(page)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new DefaultObserver<SomthingResponse>() {
                @Override
                public void onNext(@NonNull SomthingResponse response) {
                    showSomthing()
                    hideProgressDialog();

                }

                @Override
                public void onError(@NonNull Throwable e) {
                    hideProgressDialog();
                    showErrorMessage(e.getMessage());
                }

                @Override
                public void onComplete() {

                }
            });
Run Code Online (Sandbox Code Playgroud)

当用户点击按钮时,用户会将页面参数发送到查询字符串,但此请求尚未完成.用户将更改为页面参数并发送新请求.我想取消第一个请求.

谢谢

android rx-java rx-android retrofit2 rx-java2

0
推荐指数
1
解决办法
403
查看次数

动态提供值时,改造错误URL查询字符串不得包含replace块

我想使用改型获取JSON数据,但出现此错误

由以下原因引起:java.lang.IllegalArgumentException:URL查询字符串“ q = {text}&langpair = {l_from} | {l_to}”不能具有replace块。对于动态查询参数,请使用@Query。

我的代码是

    // example of my site
    // http://mytempsite.com/get?q=hello friend&langpair=en|ur

    @GET("get?q={text}&langpair={from}|{to}")
    Call<ApiService> getJsonData(@Query("text") String text,
                                 @Query("from") String from,
                                 @Query("to") String to);
Run Code Online (Sandbox Code Playgroud)

还有我的电话请求

Call<ApiService> call = apiService.getJsonData("hello word","en","ur");
Run Code Online (Sandbox Code Playgroud)

但是当我像这样静态使用时,它将起作用。

@GET("get?q=Hello Word&langpair=en|ur")
        Call<ApiService> getJsonData(@Query("text") String text,
                                     @Query("from") String from,
                                     @Query("to") String to);
Run Code Online (Sandbox Code Playgroud)

java android retrofit retrofit2

0
推荐指数
1
解决办法
1756
查看次数

APP崩溃:致命异常:io.reactivex.exceptions.CompositeException

使用retrofitI调用API获取列表,但是当没有找到数据时,我正在调用onErrorGetOccasionmethod HttpException,为此,我按照代码中所示的方式进行处理。

现在我的问题有时onErrorGetOccasion是致命的

Exception: io.reactivex.exceptions.CompositeException this error and app crash.
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

//Get Occasion API
private void callGetOccasionAPI(String pageIndex, boolean isDialogShown) {

    if (NetworkUtils.isConnected()) {
        if (mPageCount == 1 && isDialogShown)
            showProgressDialog(mContext);
        RetrofitAdapter.createRetroServiceWithSessionToken(mContext)
                .getOccasion(pageIndex)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onSuccessGetOccasion, this::onErrorGetOccasion);
    } else {
        SnackBarUtils.defaultSnackBar(getString(R.string.error_no_internet), mRootView);
    }
}

private void onErrorGetOccasion(Throwable throwable) {
    hideProgressDialog();
    if (throwable instanceof HttpException) {
        ResponseBody body = ((HttpException) throwable).response().errorBody();
        Utils.setLog(body != null ? body.toString() : null);
        try {
            if (body …
Run Code Online (Sandbox Code Playgroud)

android reactivex retrofit2

0
推荐指数
1
解决办法
1503
查看次数