2016-08-22 65 views
0

如何侦听服务器响应(简单回声“成功”;成功mysql查询后的东西。)。就像排球响应监听器一样,但是对于okhttp而言。顺便说一句,response.networkResponse()。toString()返回我需要的东西,但是我需要知道什么时候得到响应,比如抽象。如何监听服务器响应? Okhttp库

回答

1

你也许希望这样的事情:

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