Search in sources :

Example 41 with ANError

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

the class Rx2PostJSONApiTest method testJSONObjectPostRequest404.

public void testJSONObjectPostRequest404() 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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build().getJSONObjectObservable().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<JSONObject>() {

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

        @Override
        public void onNext(JSONObject 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) JSONObject(org.json.JSONObject)

Example 42 with ANError

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

the class Rx2PostJSONApiTest 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 AtomicReference<Boolean> isSubscribedRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    Rx2AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build().getJSONArrayObservable().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<JSONArray>() {

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

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

Example 43 with ANError

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

the class Rx2PostStringApiTest method testStringPostRequest404.

public void testStringPostRequest404() 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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").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 44 with ANError

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

the class Utils method logError.

public static void logError(String TAG, Throwable e) {
    if (e instanceof ANError) {
        ANError anError = (ANError) e;
        if (anError.getErrorCode() != 0) {
            // received ANError from server
            // error.getErrorCode() - the ANError code from server
            // error.getErrorBody() - the ANError body from server
            // error.getErrorDetail() - just a ANError detail
            Log.d(TAG, "onError errorCode : " + anError.getErrorCode());
            Log.d(TAG, "onError errorBody : " + anError.getErrorBody());
            Log.d(TAG, "onError errorDetail : " + anError.getErrorDetail());
        } else {
            // error.getErrorDetail() : connectionError, parseError, requestCancelledError
            Log.d(TAG, "onError errorDetail : " + anError.getErrorDetail());
        }
    } else {
        Log.d(TAG, "onError errorMessage : " + e.getMessage());
    }
}
Also used : ANError(com.androidnetworking.error.ANError)

Example 45 with ANError

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

the class RxApiTestActivity method createAnUserJSONObject.

public void createAnUserJSONObject(View view) {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("firstname", "Rohit");
        jsonObject.put("lastname", "Kumar");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    RxAndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER).addJSONObjectBody(jsonObject).build().setAnalyticsListener(new AnalyticsListener() {

        @Override
        public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
            Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
            Log.d(TAG, " bytesSent : " + bytesSent);
            Log.d(TAG, " bytesReceived : " + bytesReceived);
            Log.d(TAG, " isFromCache : " + isFromCache);
        }
    }).getJSONObjectObservable().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<JSONObject>() {

        @Override
        public void onCompleted() {
            Log.d(TAG, "onComplete Detail : createAnUserJSONObject completed");
        }

        @Override
        public void onError(Throwable e) {
            if (e instanceof ANError) {
                ANError anError = (ANError) e;
                if (anError.getErrorCode() != 0) {
                    // received ANError from server
                    // error.getErrorCode() - the ANError code from server
                    // error.getErrorBody() - the ANError body from server
                    // error.getErrorDetail() - just a ANError detail
                    Log.d(TAG, "onError errorCode : " + anError.getErrorCode());
                    Log.d(TAG, "onError errorBody : " + anError.getErrorBody());
                    Log.d(TAG, "onError errorDetail : " + anError.getErrorDetail());
                } else {
                    // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                    Log.d(TAG, "onError errorDetail : " + anError.getErrorDetail());
                }
            } else {
                Log.d(TAG, "onError errorMessage : " + e.getMessage());
            }
        }

        @Override
        public void onNext(JSONObject response) {
            Log.d(TAG, "onResponse object : " + response.toString());
            Log.d(TAG, "onResponse isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
        }
    });
}
Also used : AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) 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