改造请求在片段中等待另一个请求完成的问题

问题描述 投票:0回答:1

在我的应用程序中,我使用 Retrofit 来使用宁静的 Web 服务。 当片段 A 请求正在运行时,我正在销毁片段 A 并传递给片段 B。 片段 B 请求正在等待片段 A 请求完成!不应该啊!!!!我做错了什么?

当我输入另一个片段时,它不应该等待另一个片段响应。

当我输入另一个片段时,它不应该等待另一个片段响应。因为我已经毁掉了第一个碎片。不应该等待!!!

请帮助我!!!!!!

java android request retrofit fragment
1个回答
0
投票

我正在使用 HttpClassHelper 类。在Fragment A和Fragment B中像这样调用这个api。但是如果没有片段 A 请求完成,片段 B 请求就不会开始。

//如何调用api类片段A-B调用//

  httpRequestHelper.makeHttpRequest(call2, str, new HttpRequestHelper.HttpRequestCallback() {
            @Override
            public void onResponse(String responseBody) {
                arrayList = convertJson(responseBody);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (arrayList.size() > 0) {
                            responseToList();
                        }
                    }
                });
            }

            @Override
            public void onError(Exception e) {
            }

            @Override
            public void onLoading(boolean isLoading) {
                progressBar.setVisibility(isLoading ? View.VISIBLE : View.GONE);
            }
        });

//这是我的 api 类//

public class HttpRequestHelper {


     OkHttpClient client;
    public interface HttpRequestCallback {
        void onResponse(String responseBody);

        void onError(Exception e);

        void onLoading(boolean isLoading);
    }


    public  void makeHttpRequest(Call call,String url, final HttpRequestCallback callback) {

        client = new OkHttpClient.Builder()
                .connectTimeout(9999, TimeUnit.SECONDS)
                .readTimeout(9999, TimeUnit.SECONDS).build();

        Request request = new Request.Builder()
                .url(url)
                .build();

        call = client.newCall(request);
        callback.onLoading(true);
        Log.i("HttpRequestHelper", "Request is going on to: " + url);

        call.enqueue(new Callback() {

            @Override
            public void onResponse(Call call, Response response) {
                try {
                   if (!call.isCanceled())
                   {
                       String responseBody = response.body().string();
                       Log.d(TAG, "Response from retrofit: " + responseBody);
                       callback.onResponse(responseBody);
                   }
                } catch (Exception e) {
                    callback.onError(e);
                } finally {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onLoading(false);
                        }
                    });
                }

                Log.i("HttpRequestHelper", "Request is completed!");
            }

            @Override
            public void onFailure(Call call, IOException e) {
                callback.onError(e);
                // Ana iş parçacığında UI güncellemesi yapmak için Handler kullanıyoruz
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onLoading(false);
                    }
                });
                if (call.isCanceled()) {
                    Log.e("HttpRequestHelper", "Request was cancelled!");
                } else {
                    Log.e("HttpRequestHelper", "other larger issue, i.e. no network connection?");
                }
                //    Log.i("HttpRequestHelper", "Request is failed!");
            }

        });
    }

    public  void cancelRequest(Call call) {
           Log.i("HttpRequestHelper", "CALL IS CANCELLED!");
        if (call != null && !call.isCanceled() && !call.isExecuted()) {
            call.cancel();
        }

    }


}
© www.soinside.com 2019 - 2024. All rights reserved.