Search in sources :

Example 56 with ANError

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

the class Rx2GetStringApiTest 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 AtomicReference<Boolean> isSubscribedRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    Rx2AndroidNetworking.get(server.url("/").toString()).build().getStringObservable().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {

        @Override
        public void onSubscribe(Disposable d) {
            isSubscribedRef.set(true);
        }

        @Override
        public void onNext(String response) {
            assertTrue(false);
        }

        @Override
        public void onError(Throwable e) {
            ANError anError = (ANError) e;
            errorBodyRef.set(anError.getErrorBody());
            errorDetailRef.set(anError.getErrorDetail());
            errorCodeRef.set(anError.getErrorCode());
            latch.countDown();
        }

        @Override
        public void onComplete() {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(isSubscribedRef.get());
    assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, errorDetailRef.get());
    assertEquals("data", errorBodyRef.get());
    assertEquals(404, errorCodeRef.get().intValue());
}
Also used : Disposable(io.reactivex.disposables.Disposable) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 57 with ANError

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

the class InternalRunnable method executeSimpleRequest.

private void executeSimpleRequest() {
    Response okHttpResponse = null;
    try {
        okHttpResponse = InternalNetworking.performSimpleRequest(request);
        if (okHttpResponse == null) {
            deliverError(request, Utils.getErrorForConnection(new ANError()));
            return;
        }
        if (request.getResponseAs() == ResponseType.OK_HTTP_RESPONSE) {
            request.deliverOkHttpResponse(okHttpResponse);
            return;
        }
        if (okHttpResponse.code() >= 400) {
            deliverError(request, Utils.getErrorForServerResponse(new ANError(okHttpResponse), request, okHttpResponse.code()));
            return;
        }
        ANResponse response = request.parseResponse(okHttpResponse);
        if (!response.isSuccess()) {
            deliverError(request, response.getError());
            return;
        }
        response.setOkHttpResponse(okHttpResponse);
        request.deliverResponse(response);
    } catch (Exception e) {
        deliverError(request, Utils.getErrorForConnection(new ANError(e)));
    } finally {
        SourceCloseUtil.close(okHttpResponse, request);
    }
}
Also used : ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) ANError(com.androidnetworking.error.ANError) ANResponse(com.androidnetworking.common.ANResponse)

Example 58 with ANError

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

the class InternalRunnable method executeDownloadRequest.

private void executeDownloadRequest() {
    Response okHttpResponse;
    try {
        okHttpResponse = InternalNetworking.performDownloadRequest(request);
        if (okHttpResponse == null) {
            deliverError(request, Utils.getErrorForConnection(new ANError()));
            return;
        }
        if (okHttpResponse.code() >= 400) {
            deliverError(request, Utils.getErrorForServerResponse(new ANError(okHttpResponse), request, okHttpResponse.code()));
            return;
        }
        request.updateDownloadCompletion();
    } catch (Exception e) {
        deliverError(request, Utils.getErrorForConnection(new ANError(e)));
    }
}
Also used : ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) ANError(com.androidnetworking.error.ANError)

Example 59 with ANError

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

the class GetObjectApiTest method testResponseBodyAndObjectListGet.

public void testResponseBodyAndObjectListGet() 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.get(server.url("/").toString()).setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndObjectList(User.class, new OkHttpResponseAndParsedRequestListener<List<User>>() {

        @Override
        public void onResponse(Response okHttpResponse, List<User> userList) {
            firstNameRef.set(userList.get(0).firstName);
            lastNameRef.set(userList.get(0).lastName);
            responseBodySuccess.set(okHttpResponse.isSuccessful());
            latch.countDown();
        }

        @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) User(com.androidnetworking.model.User) 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) List(java.util.List)

Example 60 with ANError

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

the class GetObjectApiTest method testResponseBodyAndObjectGet404.

public void testResponseBodyAndObjectGet404() 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.get(server.url("/").toString()).setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndObject(User.class, new OkHttpResponseAndParsedRequestListener<User>() {

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

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