Search in sources :

Example 6 with OkHttpResponseAndStringRequestListener

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

the class MultipartStringApiTest method testResponseBodyAndStringMultipart404.

public void testResponseBodyAndStringMultipart404() 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().getAsOkHttpResponseAndString(new OkHttpResponseAndStringRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, String 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) OkHttpResponseAndStringRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndStringRequestListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 7 with OkHttpResponseAndStringRequestListener

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

the class MultipartStringApiTest method testHeaderMultipartRequest.

public void testHeaderMultipartRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("data"));
    final AtomicReference<String> responseRef = 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().getAsOkHttpResponseAndString(new OkHttpResponseAndStringRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, String response) {
            responseRef.set(response);
            responseBodySuccess.set(okHttpResponse.isSuccessful());
            headerRef.set(okHttpResponse.request().header("headerKey"));
            latch.countDown();
        }

        @Override
        public void onError(ANError anError) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(responseBodySuccess.get());
    assertEquals("data", responseRef.get());
    assertEquals("headerValue", headerRef.get());
}
Also used : ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpResponseAndStringRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndStringRequestListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with OkHttpResponseAndStringRequestListener

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

the class PostStringApiTest method testResponseBodyAndStringPost.

public void testResponseBodyAndStringPost() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("data"));
    final AtomicReference<Boolean> responseBodySuccess = new AtomicReference<>();
    final AtomicReference<String> responseStringRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndString(new OkHttpResponseAndStringRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, String response) {
            responseBodySuccess.set(okHttpResponse.isSuccessful());
            responseStringRef.set(response);
            latch.countDown();
        }

        @Override
        public void onError(ANError anError) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(responseBodySuccess.get());
    assertEquals("data", responseStringRef.get());
}
Also used : ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpResponseAndStringRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndStringRequestListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with OkHttpResponseAndStringRequestListener

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

the class GetStringApiTest method testHeaderGetRequest.

public void testHeaderGetRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("data"));
    final AtomicReference<String> responseRef = 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().getAsOkHttpResponseAndString(new OkHttpResponseAndStringRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, String response) {
            responseRef.set(response);
            responseBodySuccess.set(okHttpResponse.isSuccessful());
            headerRef.set(okHttpResponse.request().header("headerKey"));
            latch.countDown();
        }

        @Override
        public void onError(ANError anError) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(responseBodySuccess.get());
    assertEquals("data", responseRef.get());
    assertEquals("headerValue", headerRef.get());
}
Also used : ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpResponseAndStringRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndStringRequestListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ANResponse (com.androidnetworking.common.ANResponse)9 ANError (com.androidnetworking.error.ANError)9 OkHttpResponseAndStringRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndStringRequestListener)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Response (okhttp3.Response)9 MockResponse (okhttp3.mockwebserver.MockResponse)9