【问题标题】:Handle Multiple Requests in Retrofit?在改造中处理多个请求?
【发布时间】:2018-07-18 19:10:02
【问题描述】:

我正在开发一个需要请求 10 个 API 调用并在 ArrayList 中返回响应的应用程序。

我提到了一些关于多个请求的问题。然后我使用了 Retrofit 和 Rx。

但是说HTTP FAILED: java.net.SocketTimeoutException: timeout会崩溃

所以请指导我...

LOGCAT

  9657-9677 D/OkHttp: <-- HTTP FAILED: java.net.SocketTimeoutException: timeout
9657-9677 D/OkHttp: --> POST http://app.pay.com/paiay_features/recharge_bill/requestdetails.php http/1.1
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 42
9657-9657 W/System.err: io.reactivex.exceptions.OnErrorNotImplementedException: timeout
9657-9677 D/OkHttp: operaion=CheckCustomerDetails&userid=58155
    --> END POST (42-byte body)
9657-9657 W/System.err:     at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
9657-9677 D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled
9657-9657 W/System.err:     at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
9657-9657 W/System.err:     at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:77)
9657-9677 D/OkHttp: --> POST http://app.pay_features/recharge_bill/requestdetails.php http/1.1
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 42
9657-9657 W/System.err:     at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276)
9657-9677 D/OkHttp: operaion=CheckCustomerDetails&userid=61885
9657-9677 D/OkHttp: --> END POST (42-byte body)
9657-9657 W/System.err:     at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
9657-9677 D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled

代码

private RecyclerView recyclerView;
    private PersonsAdapter adapter;
    private List<Person> pList;

    private ArrayList<String> balanceList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        pList = new ArrayList<>();
        adapter = new PersonsAdapter(this, pList);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);

        Observable.zip(apiInterface.getDetails("CheckCustomerDetails", "61885"),
                apiInterface.getDetails("CheckCustoDetails", "5815"),
                apiInterface.getDetails("CheckCustoDetails", "6185"),
                (u1, u2, u3) -> {
            // prepare your returned users in a way suitable for further consumption
            // in this case I put them in a list
            return Arrays.asList(u1, u2, u3);
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(listOfUsers -> {
                    for (int i = 0; i < listOfUsers.size(); i++) {
                        String bal = listOfUsers.get(i).getUserAvailblnce().toString();
                        balanceList.add(bal);
                    }
                    preparePersons(balanceList);
                });

    }

    private void preparePersons(List<String> balanceList) {
        Log.e("preparePersons", "> >" + balanceList.size());

        Person a = new Person("Arhan", "96776", balanceList.get(0));
        pList.add(a);

        a = new Person("Raja", "93843", balanceList.get(1));
        pList.add(a);

        adapter.notifyDataSetChanged();

    }

型号

class APIClient {

    private static Retrofit retrofit = null;

    static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        retrofit = new Retrofit.Builder()
                .baseUrl("http://app.paiy.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();

        return retrofit;
    }

}

界面

interface APIInterface {

    @FormUrlEncoded
    @POST("bill/requestdetails.php")
    Observable<User> getDetails(@Field("operaion") String value, @Field("userid") String id);

}

【问题讨论】:

    标签: android rx-java retrofit2 rx-android


    【解决方案1】:

    您可能没有添加正确的呼叫适配器

    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    

    将适配器添加到您的改造对象

    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    

    【讨论】:

    • 现在出现超时错误?
    • 你调用的超时时间,大多数情况下默认超时时间是10秒。为您的 OkHttpClient 添加超时。
    • OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).addInterceptor(httpLoggingInterceptor).build()
    • 可能有 60 秒的超时,之后如果问题仍然存在,请找出超时的原因。之后可能是网络甚至服务器相关问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2018-04-12
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多