use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class Rx2GetJSONApiTest method testJSONArraySingleGetRequest404.
public void testJSONArraySingleGetRequest404() 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().getJSONArraySingle().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver<JSONArray>() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {
isSubscribedRef.set(true);
}
@Override
public void onSuccess(@NonNull JSONArray response) {
assertTrue(false);
}
@Override
public void onError(@NonNull Throwable e) {
ANError anError = (ANError) e;
errorBodyRef.set(anError.getErrorBody());
errorDetailRef.set(anError.getErrorDetail());
errorCodeRef.set(anError.getErrorCode());
latch.countDown();
}
});
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());
}
use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class Rx2GetJSONApiTest method testJSONArrayGetRequest404.
public void testJSONArrayGetRequest404() 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().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());
}
use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class Rx2GetObjectApiTest method testObjectListGetRequest404.
public void testObjectListGetRequest404() 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().getObjectListObservable(User.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<List<User>>() {
@Override
public void onSubscribe(Disposable d) {
isSubscribedRef.set(true);
}
@Override
public void onNext(List<User> userList) {
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());
}
use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class OkHttpResponseTestActivity method uploadImage.
public void uploadImage(final View view) {
AndroidNetworking.upload(ApiEndPoint.BASE_URL + ApiEndPoint.UPLOAD_IMAGE).setPriority(Priority.MEDIUM).addMultipartFile("image", new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "test.png")).setTag(this).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);
}
}).setUploadProgressListener(new UploadProgressListener() {
@Override
public void onProgress(long bytesUploaded, long totalBytes) {
Log.d(TAG, "bytesUploaded : " + bytesUploaded + " totalBytes : " + totalBytes);
Log.d(TAG, "setUploadProgressListener isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
}
}).getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONObject response) {
Log.d(TAG, "Image upload Completed");
Log.d(TAG, "onResponse object : " + response.toString());
Log.d(TAG, "onResponse isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
if (okHttpResponse.isSuccessful()) {
Log.d(TAG, "onResponse success headers : " + okHttpResponse.headers().toString());
} else {
Log.d(TAG, "onResponse not success headers : " + okHttpResponse.headers().toString());
}
}
@Override
public void onError(ANError anError) {
Utils.logError(TAG, anError);
}
});
}
use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class OkHttpResponseTestActivity 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