Search in sources :

Example 71 with ANError

use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.

the class PostJSONApiTest method testResponseBodyAndJSONArrayPost404.

public void testResponseBodyAndJSONArrayPost404() 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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndJSONArray(new OkHttpResponseAndJSONArrayRequestListener() {

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

Example 72 with ANError

use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.

the class PostJSONApiTest method testJSONArrayPostRequest404.

public void testJSONArrayPostRequest404() 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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build().getAsJSONArray(new JSONArrayRequestListener() {

        @Override
        public void onResponse(JSONArray 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) OkHttpResponseAndJSONArrayRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONArrayRequestListener) JSONArrayRequestListener(com.androidnetworking.interfaces.JSONArrayRequestListener) JSONArray(org.json.JSONArray) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 73 with ANError

use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.

the class PostJSONApiTest method testResponseBodyAndJSONObjectPost404.

public void testResponseBodyAndJSONObjectPost404() 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.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) {
            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 74 with ANError

use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.

the class PostJSONApiTest method testJSONArrayPostRequest.

public void testJSONArrayPostRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("[{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}]"));
    final AtomicReference<String> firstNameRef = new AtomicReference<>();
    final AtomicReference<String> lastNameRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build().getAsJSONArray(new JSONArrayRequestListener() {

        @Override
        public void onResponse(JSONArray response) {
            try {
                JSONObject jsonObject = response.getJSONObject(0);
                firstNameRef.set(jsonObject.getString("firstName"));
                lastNameRef.set(jsonObject.getString("lastName"));
                latch.countDown();
            } catch (JSONException e) {
                assertTrue(false);
            }
        }

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

Example 75 with ANError

use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.

the class PostJSONApiTest method testSynchronousJSONArrayPostRequest404.

@SuppressWarnings("unchecked")
public void testSynchronousJSONArrayPostRequest404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
    ANRequest request = AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build();
    ANResponse<JSONObject> response = request.executeForJSONArray();
    ANError error = response.getError();
    assertEquals("data", error.getErrorBody());
    assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, error.getErrorDetail());
    assertEquals(404, error.getErrorCode());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ANRequest(com.androidnetworking.common.ANRequest) JSONObject(org.json.JSONObject) ANError(com.androidnetworking.error.ANError)

Aggregations

ANError (com.androidnetworking.error.ANError)214 MockResponse (okhttp3.mockwebserver.MockResponse)148 CountDownLatch (java.util.concurrent.CountDownLatch)129 AtomicReference (java.util.concurrent.atomic.AtomicReference)129 Response (okhttp3.Response)77 ANResponse (com.androidnetworking.common.ANResponse)74 JSONObject (org.json.JSONObject)50 AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)42 List (java.util.List)31 User (com.androidnetworking.model.User)30 Disposable (io.reactivex.disposables.Disposable)30 JSONArray (org.json.JSONArray)29 ANRequest (com.androidnetworking.common.ANRequest)27 OkHttpResponseAndJSONObjectRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)21 User (com.jacksonandroidnetworking.model.User)20 JSONException (org.json.JSONException)18 OkHttpResponseAndJSONArrayRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndJSONArrayRequestListener)17 OkHttpResponseAndStringRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndStringRequestListener)15 JSONObjectRequestListener (com.androidnetworking.interfaces.JSONObjectRequestListener)13 JSONArrayRequestListener (com.androidnetworking.interfaces.JSONArrayRequestListener)12