处理翻新中的多个请求?

时间:2018-07-18 09:10:08

标签: android rx-java retrofit2 rx-android

我正在开发一个需要请求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);

}

1 个答案:

答案 0 :(得分:0)

您可能尚未添加正确的呼叫适配器

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

将适配器添加到改造对象

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())