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;
}
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());
}
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());
}
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);
}
}
}));
}
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());
}
}
});
}
Aggregations