2016-08-22 39 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