Search in sources :

Example 11 with OkHttpResponseAndJSONObjectRequestListener

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

the class OkHttpResponseTestActivity method checkForHeaderPost.

public void checkForHeaderPost(View view) {
    ANRequest.PostRequestBuilder postRequestBuilder = AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.CHECK_FOR_HEADER);
    postRequestBuilder.addHeaders("token", "1234");
    ANRequest anRequest = postRequestBuilder.setTag(this).setPriority(Priority.LOW).setExecutor(Executors.newSingleThreadExecutor()).build();
    anRequest.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);
        }
    });
    anRequest.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) 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 12 with OkHttpResponseAndJSONObjectRequestListener

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

the class MultipartJSONApiTest method testResponseBodyAndJSONObjectMultipart404.

public void testResponseBodyAndJSONObjectMultipart404() 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.upload(server.url("/").toString()).addMultipartParameter("key", "value").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 13 with OkHttpResponseAndJSONObjectRequestListener

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

the class MultipartJSONApiTest method testHeaderMultipartRequest.

public void testHeaderMultipartRequest() 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.upload(server.url("/").toString()).addHeaders("headerKey", "headerValue").addMultipartParameter("key", "value").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 14 with OkHttpResponseAndJSONObjectRequestListener

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

the class PostJSONApiTest method testHeaderPostRequest.

public void testHeaderPostRequest() 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.post(server.url("/").toString()).addHeaders("headerKey", "headerValue").addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").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 15 with OkHttpResponseAndJSONObjectRequestListener

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

the class PostJSONApiTest method testResponseBodyAndJSONObjectPost.

public void testResponseBodyAndJSONObjectPost() 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<Boolean> responseBodySuccess = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").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());
                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());
}
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)

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