Search in sources :

Example 1 with StringRequestListener

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

the class ApiTest method testPostRequest404.

public void testPostRequest404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("postResponse"));
    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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").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("postResponse", 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)

Example 2 with StringRequestListener

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

the class ApiTest method testUploadRequest.

public void testUploadRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("uploadTestResponse"));
    final AtomicReference<String> responseRef = 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) {
            responseRef.set(response);
            latch.countDown();
        }

        @Override
        public void onError(ANError anError) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertEquals("uploadTestResponse", responseRef.get());
}
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)

Example 3 with StringRequestListener

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

the class PostStringApiTest method testStringPostRequest.

public void testStringPostRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("data"));
    final AtomicReference<String> responseRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build().getAsString(new StringRequestListener() {

        @Override
        public void onResponse(String response) {
            responseRef.set(response);
            latch.countDown();
        }

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

Example 4 with StringRequestListener

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

the class GetStringApiTest method testStringGetRequest.

public void testStringGetRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("data"));
    final AtomicReference<String> responseRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.get(server.url("/").toString()).build().getAsString(new StringRequestListener() {

        @Override
        public void onResponse(String response) {
            responseRef.set(response);
            latch.countDown();
        }

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

Example 5 with StringRequestListener

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

the class GetStringApiTest method testStringGetRequest404.

public void testStringGetRequest404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
    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.get(server.url("/").toString()).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("data", errorBodyRef.get());
    assertEquals(404, errorCodeRef.get().intValue());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) StringRequestListener(com.androidnetworking.interfaces.StringRequestListener) OkHttpResponseAndStringRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndStringRequestListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

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