Search in sources :

Example 1 with ANError

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

the class Utils method getErrorForNetworkOnMainThreadOrConnection.

public static ANError getErrorForNetworkOnMainThreadOrConnection(Exception e) {
    ANError error = new ANError(e);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && e instanceof NetworkOnMainThreadException) {
        error.setErrorDetail(ANConstants.NETWORK_ON_MAIN_THREAD_ERROR);
    } else {
        error.setErrorDetail(ANConstants.CONNECTION_ERROR);
    }
    error.setErrorCode(0);
    return error;
}
Also used : ANError(com.androidnetworking.error.ANError) NetworkOnMainThreadException(android.os.NetworkOnMainThreadException)

Example 2 with ANError

use of com.androidnetworking.error.ANError 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 3 with ANError

use of com.androidnetworking.error.ANError 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 4 with ANError

use of com.androidnetworking.error.ANError in project android-mvp-architecture by MindorksOpenSource.

the class LoginPresenter method onServerLoginClick.

@Override
public void onServerLoginClick(String email, String password) {
    // validate email and password
    if (email == null || email.isEmpty()) {
        getMvpView().onError(R.string.empty_email);
        return;
    }
    if (!CommonUtils.isEmailValid(email)) {
        getMvpView().onError(R.string.invalid_email);
        return;
    }
    if (password == null || password.isEmpty()) {
        getMvpView().onError(R.string.empty_password);
        return;
    }
    getMvpView().showLoading();
    getCompositeDisposable().add(getDataManager().doServerLoginApiCall(new LoginRequest.ServerLoginRequest(email, password)).subscribeOn(getSchedulerProvider().io()).observeOn(getSchedulerProvider().ui()).subscribe(new Consumer<LoginResponse>() {

        @Override
        public void accept(LoginResponse response) throws Exception {
            getDataManager().updateUserInfo(response.getAccessToken(), response.getUserId(), DataManager.LoggedInMode.LOGGED_IN_MODE_SERVER, response.getUserName(), response.getUserEmail(), response.getGoogleProfilePicUrl());
            if (!isViewAttached()) {
                return;
            }
            getMvpView().hideLoading();
            getMvpView().openMainActivity();
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            if (!isViewAttached()) {
                return;
            }
            getMvpView().hideLoading();
            // handle the login error here
            if (throwable instanceof ANError) {
                ANError anError = (ANError) throwable;
                handleApiError(anError);
            }
        }
    }));
}
Also used : LoginResponse(com.mindorks.framework.mvp.data.network.model.LoginResponse) Consumer(io.reactivex.functions.Consumer) LoginRequest(com.mindorks.framework.mvp.data.network.model.LoginRequest) ANError(com.androidnetworking.error.ANError)

Example 5 with ANError

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

the class ApiTestActivity method checkCacheForCustomClient.

public void checkCacheForCustomClient(View view) {
    String url = "http://www.colorado.edu/conflict/peace/download/peace_problem.ZIP";
    AndroidNetworking.download(url, Utils.getRootDirPath(getApplicationContext()), "file1.zip").setPriority(Priority.HIGH).setTag(this).setOkHttpClient(new OkHttpClient()).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);
        }
    }).setDownloadProgressListener(new DownloadProgressListener() {

        @Override
        public void onProgress(long bytesDownloaded, long totalBytes) {
            Log.d(TAG, "bytesDownloaded : " + bytesDownloaded + " totalBytes : " + totalBytes);
            Log.d(TAG, "setDownloadProgressListener isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
        }
    }).startDownload(new DownloadListener() {

        @Override
        public void onDownloadComplete() {
            Log.d(TAG, "File download Completed");
            Log.d(TAG, "onDownloadComplete isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
        }

        @Override
        public void onError(ANError error) {
            if (error.getErrorCode() != 0) {
                // received ANError from server
                // error.getErrorCode() - the ANError code from server
                // error.getErrorBody() - the ANError body from server
                // error.getErrorDetail() - just an ANError detail
                Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
            } else {
                // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
            }
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) DownloadProgressListener(com.androidnetworking.interfaces.DownloadProgressListener) DownloadListener(com.androidnetworking.interfaces.DownloadListener) 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