如何监听服务器响应? Okhttp图书馆

时间:2016-08-22 13:55:55

标签: android okhttp

如何监听服务器响应(简单的回声"成功&#34 ;;成功的mysql查询后的东西。)。就像凌空响应监听器一样,但是对于okhttp而言。

顺便说一句,response.networkResponse()。toString()返回我需要的内容,但是我需要知道什么时候得到响应,比如凌空。

1 个答案:

答案 0 :(得分:2)

你可能想要这样的东西:

  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