Example usage for com.squareup.okhttp OkHttpClient newCall

List of usage examples for com.squareup.okhttp OkHttpClient newCall

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient newCall.

Prototype

public Call newCall(Request request) 

Source Link

Document

Prepares the request to be executed at some point in the future.

Usage

From source file:com.teddoll.movies.reciever.MovieSync.java

License:Apache License

public static synchronized void SyncData(final OkHttpClient client, final MovieProvider movieCache,
        final OnFetchCompleteListener listener) {
    if (inProcess)
        return;//from  w  w  w .j  a va2 s.co m
    inProcess = true;

    Request popRequest = new Request.Builder().url(POP_URL).addHeader("Accept", "application/json").build();
    Request rateRequest = new Request.Builder().url(RATE_URL).addHeader("Accept", "application/json").build();
    Request genresRequest = new Request.Builder().url(GENRE_URL).addHeader("Accept", "application/json")
            .build();
    requestCount = 3;
    pop = null;
    rate = null;
    genres = null;
    client.newCall(popRequest).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            onComplete(movieCache, listener);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                try {
                    JSONObject json = new JSONObject(response.body().string());
                    JSONArray array = json.optJSONArray("results");
                    pop = array != null ? array.toString() : null;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            onComplete(movieCache, listener);
        }
    });

    client.newCall(rateRequest).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            onComplete(movieCache, listener);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                try {
                    JSONObject json = new JSONObject(response.body().string());
                    JSONArray array = json.optJSONArray("results");
                    rate = array != null ? array.toString() : null;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            onComplete(movieCache, listener);
        }
    });
    client.newCall(genresRequest).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            onComplete(movieCache, listener);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                try {
                    JSONObject json = new JSONObject(response.body().string());
                    JSONArray array = json.optJSONArray("genres");
                    genres = array != null ? array.toString() : null;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            onComplete(movieCache, listener);
        }
    });

}

From source file:com.teddoll.movies.reciever.MovieSync.java

License:Apache License

public static void getVideos(final OkHttpClient client, Movie movie, final OnGetVideosListener listener) {
    if (listener == null)
        throw new IllegalArgumentException("OnGetVideosListener cannot be null");
    Request request = new Request.Builder().url(String.format(Locale.US, VIDEO_URL, movie.id))
            .addHeader("Accept", "application/json").build();

    client.newCall(request).enqueue(new Callback() {
        @Override//w  w  w.j a  v  a2 s  . c o  m
        public void onFailure(Request request, IOException e) {
            listener.onVideos(new ArrayList<Video>(0));
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                try {
                    JSONObject json = new JSONObject(response.body().string());
                    JSONArray array = json.optJSONArray("results");
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Video>>() {
                    }.getType();
                    List<Video> vids = gson.fromJson(array.toString(), type);
                    listener.onVideos(vids);
                } catch (JSONException e) {
                    listener.onVideos(new ArrayList<Video>(0));
                }
            } else {
                listener.onVideos(new ArrayList<Video>(0));
            }

        }
    });
}

From source file:com.teddoll.movies.reciever.MovieSync.java

License:Apache License

public static void getReviews(final OkHttpClient client, Movie movie, final OnGetReviewsListener listener) {
    if (listener == null)
        throw new IllegalArgumentException("OnGetReviewsListener cannot be null");
    Request request = new Request.Builder().url(String.format(Locale.US, REVIEW_URL, movie.id))
            .addHeader("Accept", "application/json").build();

    client.newCall(request).enqueue(new Callback() {
        @Override//from   w w w.ja v  a2s  .  co m
        public void onFailure(Request request, IOException e) {
            listener.onReviews(new ArrayList<Review>(0));
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                try {
                    JSONObject json = new JSONObject(response.body().string());
                    JSONArray array = json.optJSONArray("results");
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Review>>() {
                    }.getType();
                    List<Review> reviews = gson.fromJson(array.toString(), type);
                    listener.onReviews(reviews);
                } catch (JSONException e) {
                    listener.onReviews(new ArrayList<Review>(0));
                }
            } else {
                listener.onReviews(new ArrayList<Review>(0));
            }

        }
    });
}

From source file:com.teddoll.movies.reciever.MovieSync.java

License:Apache License

public static String[] loadCachedData(final OkHttpClient client) {
    String pop = "[]";
    String rate = "[]";
    String genre = "[]";
    Request popRequest = new Request.Builder().cacheControl(new CacheControl.Builder().onlyIfCached().build())
            .url(POP_URL).addHeader("Accept", "application/json").build();
    try {//from ww  w. java2 s  . c o  m
        Response forcePopCacheResponse = client.newCall(popRequest).execute();
        if (forcePopCacheResponse.code() != 504) {
            JSONObject json = new JSONObject(forcePopCacheResponse.body().string());
            JSONArray array = json.optJSONArray("results");
            pop = array != null ? array.toString() : null;
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }

    Request rateRequest = new Request.Builder().cacheControl(new CacheControl.Builder().onlyIfCached().build())
            .url(RATE_URL).addHeader("Accept", "application/json").build();
    try {
        Response forceRateCacheResponse = client.newCall(rateRequest).execute();
        if (forceRateCacheResponse.code() != 504) {
            JSONObject json = new JSONObject(forceRateCacheResponse.body().string());
            JSONArray array = json.optJSONArray("results");
            rate = array != null ? array.toString() : null;
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }

    Request genreRequest = new Request.Builder().cacheControl(new CacheControl.Builder().onlyIfCached().build())
            .url(GENRE_URL).addHeader("Accept", "application/json").build();
    try {
        Response forceGenreCacheResponse = client.newCall(genreRequest).execute();
        if (forceGenreCacheResponse.code() != 504) {
            JSONObject json = new JSONObject(forceGenreCacheResponse.body().string());
            JSONArray array = json.optJSONArray("genres");
            genre = array != null ? array.toString() : null;
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }

    return new String[] { pop, rate, genre };
}

From source file:com.urswolfer.gerrit.client.rest.http.HttpRequestExecutor.java

License:Apache License

public Response execute(OkHttpClient client, Request.Builder builder) throws IOException {
    return client.newCall(builder.build()).execute();
}

From source file:com.votzapp.app.RegistrationIntentService.java

License:Open Source License

/**
 * Persist registration to third-party servers.
 *
 * Modify this method to associate the user's GCM registration token with any server-side account
 * maintained by your application.//from   w  w  w  . j av a2s . c  om
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token, String email) {
    // Adding custom implementation
    OkHttpClient client = new OkHttpClient();

    //Sending both GCM_TOKEN and EMAIL to the server
    RequestBody requestBody = new FormEncodingBuilder().add(KEY_TOKEN, token).add(EMAIL, email).build();

    Request request = new Request.Builder().url(REGISTER_URL).post(requestBody).build();

    try {
        Response response = client.newCall(request).execute();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.xjeffrose.xio2.http.server.FileHandlerTest.java

License:Apache License

@Test
public void testHandle() throws Exception {
    Server s = Http.newServer();/*from ww w  .  ja  v a 2s. c  om*/
    s.bind(9012, new FileHandler("src/test/resources"));
    s.serve();

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url("http://localhost:9012/test.txt").build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    assertTrue(response.isSuccessful());
    assertEquals(response.code(), 200);
    assertEquals("this is a test.txt", response.body().string());
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test
public void testSsl() throws Exception {
    OkHttpClient unsafeClient = getUnsafeOkHttpClient();

    s.serve(9005, testHttpsHandler);/*from  w  w  w  .j a  v a2  s .  c o m*/

    Request request = new Request.Builder().url("https://localhost:9005/test").build();

    Response response = unsafeClient.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    assertEquals(response.code(), 200);
    assertEquals("THIS IS BODY", response.body().string());
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test
public void testSslMany() throws Exception {
    OkHttpClient unsafeClient = getUnsafeOkHttpClient();

    s.serve(9006, testHttpsHandler);//w w w. ja  va  2 s  .c o m

    Request request = new Request.Builder().url("https://localhost:9006/test").build();

    // Simulate 100 req's / second
    final int reqs = 100;

    for (int i = 0; i < reqs; i++) {
        Response response = unsafeClient.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response);

        assertTrue(response.isSuccessful());
        assertEquals(response.code(), 200);
        assertEquals("THIS IS BODY", response.body().string());
    }
}

From source file:com.xjeffrose.xio2.http.server.ServerTest.java

License:Apache License

@Test(expected = SSLException.class)
public void testSslFail() throws Exception {
    OkHttpClient unsafeClient = getUnsafeOkHttpClient();

    s.serve(9007, testHandler);//from  w  ww  .j a va  2  s  .  c o  m

    Request request = new Request.Builder().url("https://localhost:9007/test").build();

    Response response = unsafeClient.newCall(request).execute();
    assertTrue(!response.isSuccessful());
}