Search in sources :

Example 76 with Response

use of okhttp3.Response in project realm-java by realm.

the class HttpUtils method stopSyncServer.

public static void stopSyncServer() throws Exception {
    Request request = new Request.Builder().url(STOP_SERVER).build();
    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);
    Headers responseHeaders = response.headers();
    for (int i = 0; i < responseHeaders.size(); i++) {
        RealmLog.debug(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }
    RealmLog.debug(response.body().string());
}
Also used : Response(okhttp3.Response) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException)

Example 77 with Response

use of okhttp3.Response in project realm-java by realm.

the class HttpUtils method startSyncServer.

public static void startSyncServer() throws Exception {
    Request request = new Request.Builder().url(START_SERVER).build();
    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);
    Headers responseHeaders = response.headers();
    for (int i = 0; i < responseHeaders.size(); i++) {
        RealmLog.debug(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }
    RealmLog.debug(response.body().string());
    // FIXME: Server ready checking should be done in the control server side!
    if (!waitAuthServerReady()) {
        stopSyncServer();
        throw new RuntimeException("Auth server cannot be started.");
    }
}
Also used : Response(okhttp3.Response) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException)

Example 78 with Response

use of okhttp3.Response in project realm-java by realm.

the class HttpUtils method waitAuthServerReady.

// Checking the server
private static boolean waitAuthServerReady() throws InterruptedException {
    int retryTimes = 20;
    // Dummy invalid request, which will trigger a 400 (BAD REQUEST), but indicate the auth
    // server is responsive
    Request request = new Request.Builder().url(Constants.AUTH_SERVER_URL).build();
    while (retryTimes != 0) {
        Response response = null;
        try {
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return true;
            }
            RealmLog.error("Error response from auth server: %s", response.toString());
        } catch (IOException e) {
            // TODO As long as the auth server hasn't started yet, OKHttp cannot parse the response
            // correctly. At this point it is unknown weather is a bug in OKHttp or an
            // unknown host is reported. This can cause a lot of "false" errors in the log.
            RealmLog.error(e);
            Thread.sleep(500);
        } finally {
            if (response != null) {
                response.close();
            }
        }
        retryTimes--;
    }
    return false;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException)

Example 79 with Response

use of okhttp3.Response in project muzei by romannurik.

the class DownloadArtworkTask method openUri.

private InputStream openUri(Context context, Uri uri) throws IOException {
    if (uri == null) {
        throw new IllegalArgumentException("Uri cannot be empty");
    }
    String scheme = uri.getScheme();
    if (scheme == null) {
        throw new IOException("Uri had no scheme");
    }
    InputStream in = null;
    if ("content".equals(scheme)) {
        try {
            in = context.getContentResolver().openInputStream(uri);
        } catch (SecurityException e) {
            throw new FileNotFoundException("No access to " + uri + ": " + e.toString());
        }
    } else if ("file".equals(scheme)) {
        List<String> segments = uri.getPathSegments();
        if (segments != null && segments.size() > 1 && "android_asset".equals(segments.get(0))) {
            AssetManager assetManager = context.getAssets();
            StringBuilder assetPath = new StringBuilder();
            for (int i = 1; i < segments.size(); i++) {
                if (i > 1) {
                    assetPath.append("/");
                }
                assetPath.append(segments.get(i));
            }
            in = assetManager.open(assetPath.toString());
        } else {
            in = new FileInputStream(new File(uri.getPath()));
        }
    } else if ("http".equals(scheme) || "https".equals(scheme)) {
        OkHttpClient client = OkHttpClientFactory.getNewOkHttpsSafeClient();
        Request request;
        request = new Request.Builder().url(new URL(uri.toString())).build();
        Response response = client.newCall(request).execute();
        int responseCode = response.code();
        if (!(responseCode >= 200 && responseCode < 300)) {
            throw new IOException("HTTP error response " + responseCode);
        }
        in = response.body().byteStream();
    }
    if (in == null) {
        throw new FileNotFoundException("Null input stream for URI: " + uri);
    }
    return in;
}
Also used : AssetManager(android.content.res.AssetManager) OkHttpClient(okhttp3.OkHttpClient) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Request(okhttp3.Request) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) URL(java.net.URL) Response(okhttp3.Response) List(java.util.List) File(java.io.File)

Example 80 with Response

use of okhttp3.Response in project muzei by romannurik.

the class FiveHundredPxExampleArtSource method onTryUpdate.

@Override
protected void onTryUpdate(@UpdateReason int reason) throws RetryException {
    String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(final Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.url().newBuilder().addQueryParameter("consumer_key", Config.CONSUMER_KEY).build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);
        }
    }).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.500px.com/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).build();
    FiveHundredPxService service = retrofit.create(FiveHundredPxService.class);
    PhotosResponse response;
    try {
        response = service.getPopularPhotos().execute().body();
    } catch (IOException e) {
        Log.w(TAG, "Error reading 500px response", e);
        throw new RetryException();
    }
    if (response == null || response.photos == null) {
        throw new RetryException();
    }
    if (response.photos.size() == 0) {
        Log.w(TAG, "No photos returned from API.");
        scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
        return;
    }
    Random random = new Random();
    Photo photo;
    String token;
    while (true) {
        photo = response.photos.get(random.nextInt(response.photos.size()));
        token = Integer.toString(photo.id);
        if (response.photos.size() <= 1 || !TextUtils.equals(token, currentToken)) {
            break;
        }
    }
    publishArtwork(new Artwork.Builder().title(photo.name).byline(photo.user.fullname).imageUri(Uri.parse(photo.image_url)).token(token).viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://500px.com/photo/" + photo.id))).build());
    scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Artwork(com.google.android.apps.muzei.api.Artwork) Request(okhttp3.Request) Photo(com.example.muzei.examplesource500px.FiveHundredPxService.Photo) Intent(android.content.Intent) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Retrofit(retrofit2.Retrofit) Random(java.util.Random) PhotosResponse(com.example.muzei.examplesource500px.FiveHundredPxService.PhotosResponse) Interceptor(okhttp3.Interceptor)

Aggregations

Test (org.junit.Test)471 Response (okhttp3.Response)444 MockResponse (okhttp3.mockwebserver.MockResponse)380 Request (okhttp3.Request)377 ResponseBody (okhttp3.ResponseBody)351 IOException (java.io.IOException)220 DateTime (org.joda.time.DateTime)194 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)192 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)178 Response (retrofit2.Response)150 ServiceCall (com.microsoft.rest.ServiceCall)140 ServiceResponse (com.microsoft.rest.ServiceResponse)114 Observable (rx.Observable)104 Call (okhttp3.Call)103 List (java.util.List)95 RequestBody (okhttp3.RequestBody)85 PagedList (com.microsoft.azure.PagedList)80 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)78 OkHttpClient (okhttp3.OkHttpClient)78 HttpURLConnection (java.net.HttpURLConnection)47