保存OkHttp的响应 - Android -

时间:2016-10-24 18:29:49

标签: android okhttp3

我想保存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);


}

}

2 个答案:

答案 0 :(得分:1)

假设您要使用String格式的响应更新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);
}

答案 1 :(得分:0)

这是一个工作代码,任何人都有相同的问题或新的使用OkHttp,不幸的是我正在使用AsyncTask

向Jofre Mateu致谢

class CustomCardView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
        FrameLayout(context, attrs, defStyleAttr) {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)


    init {
        LayoutInflater.from(context).inflate(R.layout.view_custom_card, this, true)

        if (attrs != null) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.custom_card_view)
            if (a.hasValue(R.styleable.custom_card_view_command)) {
                var myString = a.getString(R.styleable.custom_card_view_command)
            }
            a.recycle()
        }
    }
}