如何设置从Common类获得的值(改造)

时间:2018-04-03 06:38:10

标签: android retrofit

我有retrofit和Common类,里面有fragment回调..我有Connect_and_get类。它向服务器发送请求并获取信息。我必须在{{1}中使用此信息但是我无法返回结果onResponse.How我可以做到这一点..(服务器的响应很快)

请参阅我的代码

public class Connect_and_Get {
           private int size;
         private OkHttpClient.Builder httpClient;
        private ApiService client;
        private Call<Response> call;
        private MyPreference myPreference;
        String a[] = {"secret"};
        String b[] = {"secret"};


        public int Connect_and_Get() {
            Requests request;
            request = new Requests("tasks.list", new params(20, 0, a, b, "", "", "", "", ""));
            httpClient = new OkHttpClient.Builder();

            httpClient.addInterceptor(new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    Request request = chain.request().newBuilder().addHeader("Authorization", "Bearer " + "secret").build();
                    return chain.proceed(request);
                }
            });

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Constants.baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build())
                    .build();
            client = retrofit.create(ApiService.class);
            call = client.getDocument(request);
            call.enqueue(new Callback<Response>() {
                @Override
                public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                    size = response.body().getResult().getList().size();

                }

                @Override
                public void onFailure(Call<Response> call, Throwable t) {

                }
            });

            //retruning information
            return size;
        }
    }

并且公共类的结果为0;因为它没有等待我的响应所以它返回0;

片段

      Connect_and_Get a = new Connect_and_Get();
    int getting = a.Connect_and_Get();
    Log.d("mylog", "result:"+String.valueOf(getting));

3 个答案:

答案 0 :(得分:1)

声明这样的界面

   public interface ResponseListener {

     public int onResponse(int size);

}

并在您的活动中使用以下代码

public class MainActivity extends AppCompatActivity implements ResponseListener {

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

    new Connect_and_Get().Connect_and_Get(this);

}


@Override
public int onResponse(int size) {
    // to do
    return 0;
}

}

像这样修改你的连接类

 public class Connect_and_Get {


    public int Connect_and_Get(ResponseListener responseListener) {
       // as it was

        call.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                size = response.body().getResult().getList().size();
                responseListener.onResponse(size);
            }

            @Override
            public void onFailure(Call<Response> call, Throwable t) {

            }
        });


    }
}

答案 1 :(得分:0)

您需要检查您的回复是否成功。 请检查以下代码。

call.enqueue(new Callback<Response>() {
        @Override
        public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
            if(response.isSuccessful()){
              //enter code here
            } else {
              //Error Message
            }


        }

        @Override
        public void onFailure(Call<Response> call, Throwable t) {
             Log.e("Log", "Error -> "+t.getLocalizedMessage());
        }
    });

答案 2 :(得分:0)

当api的响应准备好使用时,您可以使用像(rxbus,otto等)这样的事件总线在您的应用程序中发布事件。

改造回调示例代码:

call.enqueue(new Callback<Response>() {
        @Override
        public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
           Bus.getInstance.post(Event)

        }

        @Override
        public void onFailure(Call<Response> call, Throwable t) {

        }
    });

片段样本:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        Bus.getInstance.register(this)
    }



@Subscribe
    public void onCallDone(Event response) {
        //enter code here
    }