use of com.androidnetworking.common.ANResponse in project Fast-Android-Networking by amitshekhariitbhu.
the class ApiTestActivity method checkSynchronousCall.
public void checkSynchronousCall(View view) {
new Thread(new Runnable() {
@Override
public void run() {
String url = "http://www.colorado.edu/conflict/peace/download/peace_problem.ZIP";
ANRequest requestOne = AndroidNetworking.download(url, Utils.getRootDirPath(getApplicationContext()), "file1.zip").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()));
}
});
ANResponse<String> responseOne = requestOne.executeForDownload();
if (responseOne.isSuccess()) {
Log.d(TAG, "checkSynchronousCall : download success");
Log.d(TAG, "checkSynchronousCall : download result " + responseOne.getResult());
Response response = responseOne.getOkHttpResponse();
Log.d(TAG, "checkSynchronousCall : headers : " + response.headers().toString());
} else {
Log.d(TAG, "checkSynchronousCall : download failed");
Utils.logError(TAG, responseOne.getError());
}
ANRequest requestTwo = AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0").addQueryParameter("limit", "3").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);
}
});
ANResponse<List<User>> responseTwo = requestTwo.executeForObjectList(User.class);
if (responseTwo.isSuccess()) {
Log.d(TAG, "checkSynchronousCall : response success");
List<User> users = responseTwo.getResult();
Log.d(TAG, "userList size : " + users.size());
for (User user : users) {
Log.d(TAG, "id : " + user.id);
Log.d(TAG, "firstname : " + user.firstname);
Log.d(TAG, "lastname : " + user.lastname);
}
Response response = responseTwo.getOkHttpResponse();
Log.d(TAG, "checkSynchronousCall : headers : " + response.headers().toString());
} else {
Log.d(TAG, "checkSynchronousCall : response failed");
Utils.logError(TAG, responseTwo.getError());
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("firstname", "Rohit");
jsonObject.put("lastname", "Kumar");
} catch (JSONException e) {
e.printStackTrace();
}
ANRequest requestThree = AndroidNetworking.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);
}
});
ANResponse<JSONObject> responseThree = requestThree.executeForJSONObject();
if (responseThree.isSuccess()) {
Log.d(TAG, "checkSynchronousCall : jsonObjectANResponse success");
JSONObject jsonObjectFinal = responseThree.getResult();
Log.d(TAG, "checkSynchronousCall : jsonObjectANResponse result " + jsonObjectFinal.toString());
Response response = responseThree.getOkHttpResponse();
Log.d(TAG, "checkSynchronousCall : headers : " + response.headers().toString());
} else {
Log.d(TAG, "checkSynchronousCall : jsonObjectANResponse failed");
Utils.logError(TAG, responseThree.getError());
}
ANRequest requestFour = AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0").addQueryParameter("limit", "3").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);
}
});
ANResponse<Response> responseFour = requestFour.executeForOkHttpResponse();
if (responseFour.isSuccess()) {
Log.d(TAG, "checkSynchronousCall : okHttpResponse success");
Response okHttpResponse = responseFour.getResult();
if (okHttpResponse != null) {
if (okHttpResponse.isSuccessful()) {
Log.d(TAG, "response is successful");
try {
Log.d(TAG, "response : " + okHttpResponse.body().source().readUtf8());
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.d(TAG, "response is not successful");
}
} else {
Log.d(TAG, "response is null");
}
} else {
Log.d(TAG, "checkSynchronousCall : okHttpResponse failed");
Utils.logError(TAG, responseFour.getError());
}
}
}).start();
}
use of com.androidnetworking.common.ANResponse in project Fast-Android-Networking by amitshekhariitbhu.
the class SynchronousCall method executeSimpleRequest.
private static <T> ANResponse<T> executeSimpleRequest(ANRequest request) {
Response okHttpResponse = null;
try {
okHttpResponse = InternalNetworking.performSimpleRequest(request);
if (okHttpResponse == null) {
return new ANResponse<>(Utils.getErrorForConnection(new ANError()));
}
if (request.getResponseAs() == ResponseType.OK_HTTP_RESPONSE) {
ANResponse response = new ANResponse(okHttpResponse);
response.setOkHttpResponse(okHttpResponse);
return response;
}
if (okHttpResponse.code() >= 400) {
ANResponse response = new ANResponse<>(Utils.getErrorForServerResponse(new ANError(okHttpResponse), request, okHttpResponse.code()));
response.setOkHttpResponse(okHttpResponse);
return response;
}
ANResponse response = request.parseResponse(okHttpResponse);
response.setOkHttpResponse(okHttpResponse);
return response;
} catch (ANError se) {
return new ANResponse<>(Utils.getErrorForConnection(new ANError(se)));
} catch (Exception e) {
return new ANResponse<>(Utils.getErrorForConnection(new ANError(e)));
} finally {
SourceCloseUtil.close(okHttpResponse, request);
}
}
use of com.androidnetworking.common.ANResponse in project Fast-Android-Networking by amitshekhariitbhu.
the class SynchronousCall method executeDownloadRequest.
private static <T> ANResponse<T> executeDownloadRequest(ANRequest request) {
Response okHttpResponse;
try {
okHttpResponse = InternalNetworking.performDownloadRequest(request);
if (okHttpResponse == null) {
return new ANResponse<>(Utils.getErrorForConnection(new ANError()));
}
if (okHttpResponse.code() >= 400) {
ANResponse response = new ANResponse<>(Utils.getErrorForServerResponse(new ANError(okHttpResponse), request, okHttpResponse.code()));
response.setOkHttpResponse(okHttpResponse);
return response;
}
ANResponse response = new ANResponse(ANConstants.SUCCESS);
response.setOkHttpResponse(okHttpResponse);
return response;
} catch (ANError se) {
return new ANResponse<>(Utils.getErrorForConnection(new ANError(se)));
} catch (Exception e) {
return new ANResponse<>(Utils.getErrorForConnection(new ANError(e)));
}
}
use of com.androidnetworking.common.ANResponse 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);
}
}
use of com.androidnetworking.common.ANResponse in project Fast-Android-Networking by amitshekhariitbhu.
the class GetStringApiTest method testSyncResponseBodyGet404.
@SuppressWarnings("unchecked")
public void testSyncResponseBodyGet404() throws InterruptedException, IOException {
server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
ANRequest request = AndroidNetworking.get(server.url("/").toString()).build();
ANResponse<Response> response = request.executeForOkHttpResponse();
assertEquals("data", response.getResult().body().string());
assertEquals(404, response.getResult().code());
}
Aggregations