保存来自 OkHttp 的响应 -- Android --

Save the response from OkHttp -- Android --

我想保存 OnResponse 方法的结果以用于更新 UI 我试图将结果保存到 String var 中,然后将其调用到主线程中,但它不起作用。

这是我的代码和一些注释, 有帮助吗?

package com.example.blacknight.testokhttp;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.R.string.ok;
public class MainActivity extends AppCompatActivity {

    public final String URL_MOVIE = "http://api.themoviedb.org/3/movie/popular?api_key=" + API_KEY;
    String res_120 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(URL_MOVIE)
            .build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {

        }

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

            Log.v("BK-201 URL: " , response.body().string());

            // wanna save the result to update UI
            res_120 = response.body().string();

        }

    });


    // just for test : if the result has been saved or not
    Log.i("BK-111 : " , res_120);


}

}

假设您想使用字符串格式的响应更新 UI 中的 TextView 元素。你可以做这样的事情。我保留了您的测试日志以帮助您遵循代码,以防万一。

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

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(URL_MOVIE)
            .build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {

        }

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

            Log.v("BK-201 URL: " , response.body().string());

            // wanna save the result to update UI
            res_120 = response.body().string();
            updateUI(response.body().string());
        }
    });    
}

void updateUI(String string) {
   textView.setText(string);
   Log.i("BK-111 : " , res_120);
}

这是适用于遇到相同问题或使用 OkHttp 的新手的工作代码,不幸的是我正在使用 AsyncTask

感谢 Jofre Mateu

package com.example.blacknight.testokhttp;


import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity {

    public final String URL_MOVIE = "http://api.themoviedb.org/3/movie/popular?api_key=" + API_KEY ;


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

        new MovieTask().execute();
    }


    public class MovieTask extends AsyncTask<String , Void , String>
    {

        @Override
        protected String doInBackground(String... params) {

            try {

                OkHttpClient client = new OkHttpClient();



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


                Response response = client.newCall(request).execute();
                String res_120 = response.body().string();

                return res_120;

            } catch (Exception e ){

                return null;
            }
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            TextView textView = (TextView) findViewById(R.id.testView_test);
            textView.setText(s);
        }
    }

}