user6650650
user6650650

Reputation:

How to listen for server response ? Okhttp library

How do I listen server response ( simple echo"Success"; stuff after successful mysql queries.) . Just like the volley response listener , but for okhttp instead.

By the way , response.networkResponse().toString() returns what I need , but I need to know when I get that response , like volley.

Upvotes: 1

Views: 1890

Answers (1)

sagix
sagix

Reputation: 670

You maybe want something like that:

  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

Upvotes: 2

Related Questions