Search in sources :

Example 71 with Call

use of zipkin2.Call in project openhab-android by openhab.

the class MyWebImage method getBitmapFromUrl.

private Bitmap getBitmapFromUrl(Context context, final String url) {
    final Map<String, Object> result = new HashMap<String, Object>();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    MyAsyncHttpClient client = new MyAsyncHttpClient(context, prefs.getBoolean(Constants.PREFERENCE_SSLHOST, false), prefs.getBoolean(Constants.PREFERENCE_SSLCERT, false));
    client.setTimeout(READ_TIMEOUT);
    client.setBaseUrl(url);
    if (shouldAuth) {
        client.setBasicAuth(authUsername, authPassword);
    }
    client.get(url, new MyHttpClient.ResponseHandler() {

        @Override
        public void onFailure(Call call, int statusCode, Headers headers, byte[] responseBody, Throwable error) {
            Log.e(TAG, "Failed to get " + url + " with code " + statusCode + ":" + error);
            synchronized (result) {
                result.put("error", error);
                result.notify();
            }
        }

        @Override
        public void onSuccess(Call call, int statusCode, Headers headers, byte[] responseBody) {
            InputStream is = new ByteArrayInputStream(responseBody);
            Map headersMap = headers.toMultimap();
            boolean isSVG = headersMap.containsKey("content-type") && headersMap.get("content-type").toString().contains("svg");
            synchronized (result) {
                result.put("bitmap", getBitmapFromInputStream(isSVG, is));
                result.notify();
            }
        }
    });
    synchronized (result) {
        try {
            result.wait(60000);
        } catch (InterruptedException e) {
            Log.e(TAG, "Timeout fetching " + url);
            return null;
        }
        if (result.containsKey("error")) {
            return null;
        }
    }
    Log.i(TAG, "fetched bitmap for " + url);
    return (Bitmap) result.get("bitmap");
}
Also used : Call(okhttp3.Call) HashMap(java.util.HashMap) SharedPreferences(android.content.SharedPreferences) Headers(okhttp3.Headers) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Bitmap(android.graphics.Bitmap) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 72 with Call

use of zipkin2.Call in project openhab-android by openhab.

the class DefaultConnectionTest method testSyncResolveAbsoluteUrl.

@Test
public void testSyncResolveAbsoluteUrl() {
    MyHttpClient httpClient = testConnection.getSyncHttpClient();
    httpClient.get("http://mylocalmachine.local/rest/test", new MyHttpClient.TextResponseHandler() {

        @Override
        public void onFailure(Call call, int statusCode, Headers headers, String responseBody, Throwable error) {
            assertEquals("http://mylocalmachine.local/rest/test", call.request().url().toString());
        }

        @Override
        public void onSuccess(Call call, int statusCode, Headers headers, String responseBody) {
            fail("The request should never succeed in tests.");
        }
    });
}
Also used : Call(okhttp3.Call) MyHttpClient(org.openhab.habdroid.util.MyHttpClient) Headers(okhttp3.Headers) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 73 with Call

use of zipkin2.Call in project openhab-android by openhab.

the class MySyncHttpClientTest method testMethodErrorResponse.

/**
 * Unit test against Issue #315 "Crash when connection could not be established"
 */
@Test
public void testMethodErrorResponse() {
    MySyncHttpClient httpClient = new MySyncHttpClient(null, false, true);
    httpClient.setBaseUrl("https://demo.test");
    String host = "just.a.local.url.local";
    Response resp = httpClient.method("https://" + host, "GET", new HashMap<String, String>(), null, "", new MyHttpClient.ResponseHandler() {

        public void onFailure(Call call, int statusCode, Headers headers, byte[] responseBody, Throwable error) {
        }

        public void onSuccess(Call call, int statusCode, Headers headers, byte[] responseBody) {
        }
    });
    assertEquals(500, resp.code());
    assertTrue(resp.message().startsWith(UnknownHostException.class.getName()));
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Headers(okhttp3.Headers) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 74 with Call

use of zipkin2.Call in project DVECTN by tanaponudom.

the class NetworkConnectionManager method callSrever_getstuemp.

// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void callSrever_getstuemp(final OnNetworkCallback_getstuemp listener, String dep_id) {
    Gson gson = new GsonBuilder().setLenient().create();
    final Retrofit retrofit = new Retrofit.Builder().baseUrl(Fragment_login.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build();
    APISERVER callapi = retrofit.create(APISERVER.class);
    Call call = callapi.get_getstuemp(dep_id);
    call.enqueue(new Callback<List<POJO_getstuemp>>() {

        @Override
        public void onResponse(Call<List<POJO_getstuemp>> call, Response<List<POJO_getstuemp>> response) {
            try {
                List<POJO_getstuemp> getstuemp = (List<POJO_getstuemp>) response.body();
                if (response.code() != 200) {
                    // Log.e("Network connected","Response code = "+response.code());
                    ResponseBody responseBody = response.errorBody();
                    if (responseBody != null) {
                        listener.onBodyError(responseBody);
                    } else if (responseBody == null) {
                        listener.onBodyErrorIsNull();
                    }
                // Toast.makeText(, ""+loginRes.getAccesstoken(), Toast.LENGTH_SHORT).show();
                // Log.e("Network connected","Response code = "+loginRes.getAccesstoken());
                } else {
                    listener.onResponse(getstuemp);
                }
            } catch (Exception e) {
            // Log.e("Network connect error",e.getMessage());
            }
        }

        @Override
        public void onFailure(Call<List<POJO_getstuemp>> call, Throwable t) {
            Log.e("NT", t.getMessage());
            try {
                listener.onFailure(t);
            } catch (Exception e) {
                listener.onFailure(t);
            // Log.e("Network connectLogin",t.getMessage());
            }
        }
    });
}
Also used : Call(retrofit2.Call) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) List(java.util.List) POJO_getstuemp(ctn.example.user.dvectn.POJO.POJO_getstuemp)

Example 75 with Call

use of zipkin2.Call in project DVECTN by tanaponudom.

the class NetworkConnectionManager method callServer_row_teacher.

// //////////////////////////////////////////////////////////////////////////////////////////////  ////////////////////////////////////////////////////////////////////////////////////////////////
public void callServer_row_teacher(final OnNetworkCallback_row_teacher listener, int member_id, String score, int spn) {
    Gson gson = new GsonBuilder().setLenient().create();
    final Retrofit retrofit = new Retrofit.Builder().baseUrl(Fragment_login.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build();
    APISERVER callapi = retrofit.create(APISERVER.class);
    Call call = callapi.get_away_font_me(member_id, score, spn);
    call.enqueue(new Callback<POJO_row_teacher>() {

        @Override
        public void onResponse(Call<POJO_row_teacher> call, Response<POJO_row_teacher> response) {
            try {
                POJO_row_teacher row_roi = (POJO_row_teacher) response.body();
                if (response.code() != 200) {
                    // Log.e("Network connected","Response code = "+response.code());
                    ResponseBody responseBody = response.errorBody();
                    if (responseBody != null) {
                        listener.onBodyError(responseBody);
                    } else if (responseBody == null) {
                        listener.onBodyErrorIsNull();
                    }
                // Toast.makeText(, ""+loginRes.getAccesstoken(), Toast.LENGTH_SHORT).show();
                // Log.e("Network connected","Response code = "+loginRes.getAccesstoken());
                } else {
                    listener.onResponse(row_roi);
                }
            } catch (Exception e) {
            // Log.e("Network connect error",e.getMessage());
            }
        }

        @Override
        public void onFailure(Call<POJO_row_teacher> call, Throwable t) {
            Log.e("NT", t.getMessage());
            try {
                listener.onFailure(t);
            } catch (Exception e) {
                listener.onFailure(t);
            // Log.e("Network connectLogin",t.getMessage());
            }
        }
    });
}
Also used : Call(retrofit2.Call) POJO_row_teacher(ctn.example.user.dvectn.POJO.POJO_row_teacher) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit)

Aggregations

Call (okhttp3.Call)409 Response (okhttp3.Response)309 Request (okhttp3.Request)282 IOException (java.io.IOException)232 Call (retrofit2.Call)134 Callback (okhttp3.Callback)133 OkHttpClient (okhttp3.OkHttpClient)98 Test (org.junit.Test)88 ResponseBody (okhttp3.ResponseBody)76 RequestBody (okhttp3.RequestBody)58 Retrofit (retrofit2.Retrofit)48 Gson (com.google.gson.Gson)47 Response (retrofit2.Response)47 File (java.io.File)44 Headers (okhttp3.Headers)41 Callback (retrofit2.Callback)41 GsonBuilder (com.google.gson.GsonBuilder)40 JSONObject (org.json.JSONObject)39 MockResponse (okhttp3.mockwebserver.MockResponse)38 List (java.util.List)35