use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class PostJSONApiTest method testSynchronousJSONObjectPostRequest404.
@SuppressWarnings("unchecked")
public void testSynchronousJSONObjectPostRequest404() throws InterruptedException {
server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
ANRequest request = AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build();
ANResponse<JSONObject> response = request.executeForJSONObject();
ANError error = response.getError();
assertEquals("data", error.getErrorBody());
assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, error.getErrorDetail());
assertEquals(404, error.getErrorCode());
}
use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class PostJSONApiTest method testResponseBodyAndJSONArrayPost.
public void testResponseBodyAndJSONArrayPost() 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().getAsOkHttpResponseAndJSONArray(new OkHttpResponseAndJSONArrayRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONArray response) {
try {
JSONObject jsonObject = response.getJSONObject(0);
firstNameRef.set(jsonObject.getString("firstName"));
lastNameRef.set(jsonObject.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.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class PostStringApiTest method testHeaderPostRequest.
public void testHeaderPostRequest() throws InterruptedException {
server.enqueue(new MockResponse().setBody("data"));
final AtomicReference<String> responseRef = 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().getAsOkHttpResponseAndString(new OkHttpResponseAndStringRequestListener() {
@Override
public void onResponse(Response okHttpResponse, String response) {
responseRef.set(response);
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
}
@Override
public void onError(ANError anError) {
assertTrue(false);
}
});
assertTrue(latch.await(2, SECONDS));
assertTrue(responseBodySuccess.get());
assertEquals("data", responseRef.get());
assertEquals("headerValue", headerRef.get());
}
use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class PostStringApiTest method testStringPostRequest.
public void testStringPostRequest() throws InterruptedException {
server.enqueue(new MockResponse().setBody("data"));
final AtomicReference<String> responseRef = 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) {
responseRef.set(response);
latch.countDown();
}
@Override
public void onError(ANError anError) {
assertTrue(false);
}
});
assertTrue(latch.await(2, SECONDS));
assertEquals("data", responseRef.get());
}
use of com.androidnetworking.error.ANError in project Fast-Android-Networking by amitshekhariitbhu.
the class GetJSONApiTest method testResponseBodyAndJSONArrayGet.
public void testResponseBodyAndJSONArrayGet() 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().getAsOkHttpResponseAndJSONArray(new OkHttpResponseAndJSONArrayRequestListener() {
@Override
public void onResponse(Response okHttpResponse, JSONArray response) {
try {
JSONObject jsonObject = response.getJSONObject(0);
firstNameRef.set(jsonObject.getString("firstName"));
lastNameRef.set(jsonObject.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