繁体   English   中英

OkHttp3 response.body().string() 总是返回空

[英]OkHttp3 response.body().string() always returns empty


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Reader;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity {

    TextView title;
    TextView instructions;

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

        title = findViewById(R.id.titleView);
        instructions = findViewById(R.id.instructionsView);

        OkHttpClient client = new OkHttpClient();

        String url = "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/findByIngredients?number=1&ranking=1&ignorePantry=false&ingredients=apple%252Csugar%252Csalt";
        Request request = new Request.Builder()
                .url(url)
                .get()
                .addHeader("x-rapidapi-host", "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com")
                .addHeader("x-rapidapi-key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    String myResponse = response.body().string();
                    Log.i("string", myResponse);
                }
            }
        });
    }
}

日志 output for.toString(): 2020-05-04 19:55:51.801 9894-10024/com.example.testapi I/string: okhttp3.internal.Z80791B3AE7002CB88C246876D9FAA18F4796.

日志 output for.string(): 2020-05-04 20:00:39.430 10118-10143/com.example.testapi I/string: []

我知道 OkHttp3 如何在调用 first.body() 后立即清空支持资源,但对我来说它仍然是空的。

我怎样才能解决这个问题?

调用response.body().string()会消耗 body - 因此,您不能第二次调用它。 您的代码片段之外的代码中,必须至少剩下一个response.body()

响应正文只能使用一次。

https://square.github.io/okhttp/3.x/okhttp/index.html?okhttp3/ResponseBody.html

使固定

在您的实现代码中的某处, response.body()已经被调用过一次。 而是替换为response.peekBody()

如果需要进一步处理,解决方案是将其存储在变量中。

         @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    String myResponse = response.peekBody(99999L).string();
                    Log.i("string", myResponse);
                }
            }

暂无
暂无

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

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