use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.
the class OkHttpResponseTestActivity method createAnUser.
public void createAnUser(View view) {
AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER).addBodyParameter("firstname", "Suman").addBodyParameter("lastname", "Shekhar").setTag(this).setPriority(Priority.LOW).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);
}
}).getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONObject response) {
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.interfaces.OkHttpResponseAndJSONObjectRequestListener 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.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.
the class MultipartJSONApiTest method testResponseBodyAndJSONObjectMultipart.
public void testResponseBodyAndJSONObjectMultipart() throws InterruptedException {
server.enqueue(new MockResponse().setBody("{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}"));
final AtomicReference<String> firstNameRef = new AtomicReference<>();
final AtomicReference<String> lastNameRef = new AtomicReference<>();
final AtomicReference<Boolean> responseBodySuccess = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
AndroidNetworking.upload(server.url("/").toString()).addMultipartParameter("key", "value").setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONObject response) {
try {
firstNameRef.set(response.getString("firstName"));
lastNameRef.set(response.getString("lastName"));
responseBodySuccess.set(okHttpResponse.isSuccessful());
latch.countDown();
} catch (JSONException e) {
assertTrue(false);
}
}
@Override
public void onError(ANError anError) {
assertTrue(false);
}
});
assertTrue(latch.await(2, SECONDS));
assertTrue(responseBodySuccess.get());
assertEquals("Amit", firstNameRef.get());
assertEquals("Shekhar", lastNameRef.get());
}
use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.
the class PostJSONApiTest method testResponseBodyAndJSONObjectPost404.
public void testResponseBodyAndJSONObjectPost404() throws InterruptedException {
server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
final AtomicReference<String> errorBodyRef = new AtomicReference<>();
final AtomicReference<Integer> errorCodeRef = new AtomicReference<>();
final AtomicReference<String> errorDetailRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONObject 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("data", errorBodyRef.get());
assertEquals(404, errorCodeRef.get().intValue());
}
use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.
the class GetJSONApiTest method testResponseBodyAndJSONObjectGet.
public void testResponseBodyAndJSONObjectGet() throws InterruptedException {
server.enqueue(new MockResponse().setBody("{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}"));
final AtomicReference<String> firstNameRef = new AtomicReference<>();
final AtomicReference<String> lastNameRef = new AtomicReference<>();
final AtomicReference<Boolean> responseBodySuccess = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
AndroidNetworking.get(server.url("/").toString()).setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONObject response) {
try {
firstNameRef.set(response.getString("firstName"));
lastNameRef.set(response.getString("lastName"));
responseBodySuccess.set(okHttpResponse.isSuccessful());
latch.countDown();
} catch (JSONException e) {
assertTrue(false);
}
}
@Override
public void onError(ANError anError) {
assertTrue(false);
}
});
assertTrue(latch.await(2, SECONDS));
assertTrue(responseBodySuccess.get());
assertEquals("Amit", firstNameRef.get());
assertEquals("Shekhar", lastNameRef.get());
}
Aggregations