Android如何使用OkHttp从Callback获取响应字符串? [英] Android how to get response string from Callback using OkHttp?

查看:136
本文介绍了Android如何使用OkHttp从Callback获取响应字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

    OkHttpClient okHttpClient = new OkHttpClient();

    Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();

    Callback callback = new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {

        }
    };

    okHttpClient.newCall(request).enqueue(callback);

    String responseString;

在上面的代码中,我想将onResponse()方法中的response.body().string()值存储在变量responseString中,但是我无法访问它.

In the above code, I want to store the value of response.body().string() from the onResponse() method in the variable responseString, however I can't access it.

推荐答案

我认为您想做的事情是这样的:

I think what you want to do is something like this:

public class MyActivity extends Activity implements Callback , View.OnClickListener {


 @Override
 public void onCreate(Bundle savedState) {
   super.onCreate(savedState);
   findViewById(R.id.DoHttp).setOnClickListener(this);
 } 

 @Override
 public void onClick(View v) {
   if (v.getId(() == R.id.DoHttp) {

     OkHttpClient okHttpClient = new OkHttpClient();

     Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
     okHttpClient.newCall(request).enqueue(this);
   }
 }


 @Override
 public void onFailure(Request request, IOException e) {
  //do something to indicate error
 }

  @Override
  public void onResponse(Response response) throws IOException {
    if (response.isSuccessful()) {
     parse(response.body().string());
    }
  }

  private void parse(String response) {
   //do something with response
  } 
}

在上述活动中,我们实现了回调,然后在创建okhttp请求时,将其传递给我们自己的实例(此实例),这样我们就可以让oktthp回调该类,我们可以只完成一个内部类一样容易,但是这减少了我们必须制作的类的数量.我使用按钮单击来说明何时进行Http调用,但这可能是其他时间,例如,它可能在首次创建屏幕时(在onCreate中)发生.小心屏幕旋转.同样,这也假设回调是在主线程上完成的,我认为应该如此,但是我不是很肯定,因为我使用okttp的方式与您不同.如果它没有在主线程上返回响应的结果,则可以调用runOnUiThread()并将其传递给Runnable来执行更新视图的工作.

In the above activity we implement Callback and then when we create the okhttp request, we pass it an instance of ourself (this) and that way we can get oktthp to call back the class, we could have done an inner class just as easily but this reduces the # of classes we have to make. I used a button click to illustrate when the Http call is made but that could be some other time, for instance it could happen when the screen is first created (in onCreate). Be careful though of screen rotations. Also this assumes that the callback is done on the main thread which I think it would be but I'm not positive as I use okttp in a different way than you. If it does not return the results on the response on the main thread then you can call runOnUiThread() and pass it a Runnable that does the work of updating the views.

这篇关于Android如何使用OkHttp从Callback获取响应字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆