use of com.androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.
the class GetObjectApiTest method testSynchronousObjectListGetRequest.
@SuppressWarnings("unchecked")
public void testSynchronousObjectListGetRequest() throws InterruptedException, JSONException {
server.enqueue(new MockResponse().setBody("[{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}]"));
ANRequest request = AndroidNetworking.get(server.url("/").toString()).build();
ANResponse<List<User>> response = request.executeForObjectList(User.class);
User user = response.getResult().get(0);
assertEquals("Amit", user.firstName);
assertEquals("Shekhar", user.lastName);
}
use of com.androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.
the class GetObjectApiTest method testObjectGetRequest.
public void testObjectGetRequest() throws InterruptedException {
server.enqueue(new MockResponse().setBody("{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}"));
final AtomicReference<String> firstNameRef = new AtomicReference<>();
final AtomicReference<String> lastNameRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
AndroidNetworking.get(server.url("/").toString()).build().getAsObject(User.class, new ParsedRequestListener<User>() {
@Override
public void onResponse(User user) {
firstNameRef.set(user.firstName);
lastNameRef.set(user.lastName);
latch.countDown();
}
@Override
public void onError(ANError anError) {
assertTrue(false);
}
});
assertTrue(latch.await(2, SECONDS));
assertEquals("Amit", firstNameRef.get());
assertEquals("Shekhar", lastNameRef.get());
}
use of com.androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.
the class GetObjectApiTest method testSynchronousObjectGetRequest404.
@SuppressWarnings("unchecked")
public void testSynchronousObjectGetRequest404() throws InterruptedException {
server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
ANRequest request = AndroidNetworking.get(server.url("/").toString()).build();
ANResponse<User> response = request.executeForObject(User.class);
ANError error = response.getError();
assertEquals("data", error.getErrorBody());
assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, error.getErrorDetail());
assertEquals(404, error.getErrorCode());
}
use of com.androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.
the class GetObjectApiTest method testHeaderGetRequest.
public void testHeaderGetRequest() 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.get(server.url("/").toString()).addHeaders("headerKey", "headerValue").setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndObject(User.class, new OkHttpResponseAndParsedRequestListener<User>() {
@Override
public void onResponse(Response okHttpResponse, User user) {
firstNameRef.set(user.firstName);
lastNameRef.set(user.lastName);
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("Amit", firstNameRef.get());
assertEquals("Shekhar", lastNameRef.get());
assertEquals("headerValue", headerRef.get());
}
use of com.androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.
the class PostObjectApiTest method testSynchronousObjectPostRequest.
@SuppressWarnings("unchecked")
public void testSynchronousObjectPostRequest() throws InterruptedException, JSONException {
server.enqueue(new MockResponse().setBody("{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}"));
ANRequest request = AndroidNetworking.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build();
ANResponse<User> response = request.executeForObject(User.class);
assertEquals("Amit", response.getResult().firstName);
assertEquals("Shekhar", response.getResult().lastName);
}
Aggregations