Search in sources :

Example 6 with StringRequestListener

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

the class ApiTest method testUploadRequest404.

public void testUploadRequest404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("uploadTestResponse"));
    final AtomicReference<String> errorDetailRef = new AtomicReference<>();
    final AtomicReference<String> errorBodyRef = new AtomicReference<>();
    final AtomicReference<Integer> errorCodeRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.upload(server.url("/").toString()).addMultipartParameter("key", "value").build().getAsString(new StringRequestListener() {

        @Override
        public void onResponse(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("uploadTestResponse", errorBodyRef.get());
    assertEquals(404, errorCodeRef.get().intValue());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) StringRequestListener(com.androidnetworking.interfaces.StringRequestListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ANError (com.androidnetworking.error.ANError)6 StringRequestListener (com.androidnetworking.interfaces.StringRequestListener)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 MockResponse (okhttp3.mockwebserver.MockResponse)6