Search in sources :

Example 6 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class GetJSONApiTest method testResponseBodyAndJSONObjectGet404.

public void testResponseBodyAndJSONObjectGet404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
    final AtomicReference<String> errorBodyRef = new AtomicReference<>();
    final AtomicReference<Integer> errorCodeRef = new AtomicReference<>();
    final AtomicReference<String> errorDetailRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.get(server.url("/").toString()).setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, JSONObject response) {
            assertTrue(false);
        }

        @Override
        public void onError(ANError anError) {
            errorBodyRef.set(anError.getErrorBody());
            errorDetailRef.set(anError.getErrorDetail());
            errorCodeRef.set(anError.getErrorCode());
            latch.countDown();
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, errorDetailRef.get());
    assertEquals("data", errorBodyRef.get());
    assertEquals(404, errorCodeRef.get().intValue());
}
Also used : ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) JSONObject(org.json.JSONObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Example 7 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class GetJSONApiTest method testHeaderGetRequest.

public void testHeaderGetRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}"));
    final AtomicReference<String> firstNameRef = new AtomicReference<>();
    final AtomicReference<String> lastNameRef = new AtomicReference<>();
    final AtomicReference<String> headerRef = new AtomicReference<>();
    final AtomicReference<Boolean> responseBodySuccess = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.get(server.url("/").toString()).addHeaders("headerKey", "headerValue").setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, JSONObject response) {
            try {
                firstNameRef.set(response.getString("firstName"));
                lastNameRef.set(response.getString("lastName"));
                responseBodySuccess.set(okHttpResponse.isSuccessful());
                headerRef.set(okHttpResponse.request().header("headerKey"));
                latch.countDown();
            } catch (JSONException e) {
                assertTrue(false);
            }
        }

        @Override
        public void onError(ANError anError) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(responseBodySuccess.get());
    assertEquals("Amit", firstNameRef.get());
    assertEquals("Shekhar", lastNameRef.get());
    assertEquals("headerValue", headerRef.get());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) JSONException(org.json.JSONException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch) ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) JSONObject(org.json.JSONObject) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Example 8 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class OkHttpResponseTestActivity method checkForHeaderGet.

public void checkForHeaderGet(View view) {
    ANRequest.GetRequestBuilder getRequestBuilder = new ANRequest.GetRequestBuilder(ApiEndPoint.BASE_URL + ApiEndPoint.CHECK_FOR_HEADER);
    getRequestBuilder.addHeaders("token", "1234").setTag(this).setPriority(Priority.LOW).build().setAnalyticsListener(new AnalyticsListener() {

        @Override
        public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
            Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
            Log.d(TAG, " bytesSent : " + bytesSent);
            Log.d(TAG, " bytesReceived : " + bytesReceived);
            Log.d(TAG, " isFromCache : " + isFromCache);
        }
    }).getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, JSONObject response) {
            Log.d(TAG, "onResponse object : " + response.toString());
            if (okHttpResponse.isSuccessful()) {
                Log.d(TAG, "onResponse success headers : " + okHttpResponse.headers().toString());
            } else {
                Log.d(TAG, "onResponse not success headers : " + okHttpResponse.headers().toString());
            }
        }

        @Override
        public void onError(ANError anError) {
            Utils.logError(TAG, anError);
        }
    });
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) ANRequest(com.androidnetworking.common.ANRequest) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) ANError(com.androidnetworking.error.ANError) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Example 9 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class OkHttpResponseTestActivity method createAnUserJSONObject.

public void createAnUserJSONObject(View view) {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("firstname", "Rohit");
        jsonObject.put("lastname", "Kumar");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER).addJSONObjectBody(jsonObject).setTag(this).setPriority(Priority.LOW).build().setAnalyticsListener(new AnalyticsListener() {

        @Override
        public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
            Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
            Log.d(TAG, " bytesSent : " + bytesSent);
            Log.d(TAG, " bytesReceived : " + bytesReceived);
            Log.d(TAG, " isFromCache : " + isFromCache);
        }
    }).getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, JSONObject response) {
            Log.d(TAG, "onResponse object : " + response.toString());
            Log.d(TAG, "onResponse isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
            if (okHttpResponse.isSuccessful()) {
                Log.d(TAG, "onResponse success headers : " + okHttpResponse.headers().toString());
            } else {
                Log.d(TAG, "onResponse not success headers : " + okHttpResponse.headers().toString());
            }
        }

        @Override
        public void onError(ANError anError) {
            Utils.logError(TAG, anError);
        }
    });
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ANError(com.androidnetworking.error.ANError) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Example 10 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class OkHttpResponseTestActivity method disableGzipForCustomRequest.

public void disableGzipForCustomRequest(View view) {
    AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER).addBodyParameter("firstname", "Amit").addBodyParameter("lastname", "Shekhar").setTag(this).setOkHttpClient(new OkHttpClient()).setPriority(Priority.LOW).build().setAnalyticsListener(new AnalyticsListener() {

        @Override
        public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
            Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
            Log.d(TAG, " bytesSent : " + bytesSent);
            Log.d(TAG, " bytesReceived : " + bytesReceived);
            Log.d(TAG, " isFromCache : " + isFromCache);
        }
    }).getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, JSONObject response) {
            Log.d(TAG, "onResponse object : " + response.toString());
            Log.d(TAG, "onResponse isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
            if (okHttpResponse.isSuccessful()) {
                Log.d(TAG, "onResponse success headers : " + okHttpResponse.headers().toString());
            } else {
                Log.d(TAG, "onResponse not success headers : " + okHttpResponse.headers().toString());
            }
        }

        @Override
        public void onError(ANError anError) {
            Utils.logError(TAG, anError);
        }
    });
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) OkHttpClient(okhttp3.OkHttpClient) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) ANError(com.androidnetworking.error.ANError) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Aggregations

ANResponse (com.androidnetworking.common.ANResponse)15 ANError (com.androidnetworking.error.ANError)15 OkHttpResponseAndJSONObjectRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)15 Response (okhttp3.Response)15 JSONObject (org.json.JSONObject)15 CountDownLatch (java.util.concurrent.CountDownLatch)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 MockResponse (okhttp3.mockwebserver.MockResponse)9 JSONException (org.json.JSONException)7 AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)6 ANRequest (com.androidnetworking.common.ANRequest)2 UploadProgressListener (com.androidnetworking.interfaces.UploadProgressListener)1 File (java.io.File)1 OkHttpClient (okhttp3.OkHttpClient)1