简体   繁体   English

保存来自OkHttp的响应-Android-

[英]Save the response from OkHttp — Android --

I wanna to save the result from OnResponse method to use it for updating the UI i tried to save the result into String var then call it into main thread but it doesn't work . 我想从OnResponse方法中保存结果以用于更新UI,我试图将结果保存到String var中,然后将其调用到主线程中,但它不起作用。

here's my code with some comments , any help ? 这是我的代码,带有一些注释,有帮助吗?

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);


}

} }

Let's say you want to update a TextView element in you UI with the response in a String format. 假设您要使用字符串格式的响应来更新UI中的TextView元素。 You could do something like this. 你可以做这样的事情。 I keeped your test log to help you follow the code, just in case. 我保留了您的测试日志以帮助您遵循代码,以防万一。

@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);
}

Here's a working code for anyone have the same problem or new on using OkHttp , Unfortunately i'm using AsyncTask 这是一个有效的代码,适用于任何有相同问题或使用OkHttp的新人,不幸的是,我正在使用AsyncTask

Thaks to 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);
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM