gpt4 book ai didi

android - 如何监听服务器响应? Okhttp库

转载 作者:行者123 更新时间:2023-11-29 19:37:01 25 4
gpt4 key购买 nike

我如何监听服务器响应(简单的 echo"Success";mysql 查询成功后的内容。)。就像 volley response listener ,但是用于 okhttp 。

顺便说一句,response.networkResponse().toString() 会返回我需要的内容,但我需要知道何时收到该响应,例如 volley。

最佳答案

你可能想要这样的东西:

  private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();

client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
}

@Override public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());
}
});
}

https://github.com/square/okhttp/wiki/Recipes

关于android - 如何监听服务器响应? Okhttp库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39081447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com