use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.
the class OkHttpResponseTestActivity method checkForHeaderPost.
public void checkForHeaderPost(View view) {
ANRequest.PostRequestBuilder postRequestBuilder = AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.CHECK_FOR_HEADER);
postRequestBuilder.addHeaders("token", "1234");
ANRequest anRequest = postRequestBuilder.setTag(this).setPriority(Priority.LOW).setExecutor(Executors.newSingleThreadExecutor()).build();
anRequest.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);
}
});
anRequest.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 MultipartJSONApiTest method testResponseBodyAndJSONObjectMultipart404.
public void testResponseBodyAndJSONObjectMultipart404() 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.upload(server.url("/").toString()).addMultipartParameter("key", "value").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 MultipartJSONApiTest method testHeaderMultipartRequest.
public void testHeaderMultipartRequest() 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<String> headerRef = new AtomicReference<>();
final AtomicReference<Boolean> responseBodySuccess = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
AndroidNetworking.upload(server.url("/").toString()).addHeaders("headerKey", "headerValue").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());
headerRef.set(okHttpResponse.request().header("headerKey"));
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());
assertEquals("headerValue", headerRef.get());
}
use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.
the class PostJSONApiTest method testHeaderPostRequest.
public void testHeaderPostRequest() 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<String> headerRef = new AtomicReference<>();
final AtomicReference<Boolean> responseBodySuccess = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
AndroidNetworking.post(server.url("/").toString()).addHeaders("headerKey", "headerValue").addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").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());
headerRef.set(okHttpResponse.request().header("headerKey"));
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());
assertEquals("headerValue", headerRef.get());
}
use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.
the class PostJSONApiTest method testResponseBodyAndJSONObjectPost.
public void testResponseBodyAndJSONObjectPost() 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.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) {
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