Search in sources :

Example 16 with User

use of com.jacksonandroidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class JacksonPostObjectApiTest method testObjectPostRequest.

public void testObjectPostRequest() 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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) User(com.jacksonandroidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 17 with User

use of com.jacksonandroidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class JacksonPostObjectApiTest method testObjectListPostRequest.

public void testObjectListPostRequest() 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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").build().getAsObjectList(User.class, new ParsedRequestListener<List<User>>() {

        @Override
        public void onResponse(List<User> userList) {
            firstNameRef.set(userList.get(0).firstName);
            lastNameRef.set(userList.get(0).lastName);
            latch.countDown();
        }

        @Override
        public void onError(ANError anError) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertEquals("Amit", firstNameRef.get());
    assertEquals("Shekhar", lastNameRef.get());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) User(com.jacksonandroidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 18 with User

use of com.jacksonandroidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class JacksonPostObjectApiTest method testSynchronousObjectListPostRequest.

@SuppressWarnings("unchecked")
public void testSynchronousObjectListPostRequest() 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<List<User>> response = request.executeForObjectList(User.class);
    User user = response.getResult().get(0);
    assertEquals("Amit", user.firstName);
    assertEquals("Shekhar", user.lastName);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ANRequest(com.androidnetworking.common.ANRequest) User(com.jacksonandroidnetworking.model.User) List(java.util.List)

Example 19 with User

use of com.jacksonandroidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class JacksonGetObjectApiTest method testObjectGetRequest404.

public void testObjectGetRequest404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
    final AtomicReference<String> errorDetailRef = new AtomicReference<>();
    final AtomicReference<String> errorBodyRef = new AtomicReference<>();
    final AtomicReference<Integer> errorCodeRef = 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) {
            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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) User(com.jacksonandroidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 20 with User

use of com.jacksonandroidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class JacksonGetObjectApiTest 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());
}
Also used : ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) User(com.jacksonandroidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

User (com.jacksonandroidnetworking.model.User)24 MockResponse (okhttp3.mockwebserver.MockResponse)24 ANError (com.androidnetworking.error.ANError)20 CountDownLatch (java.util.concurrent.CountDownLatch)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 ANResponse (com.androidnetworking.common.ANResponse)10 List (java.util.List)10 Response (okhttp3.Response)10 ANRequest (com.androidnetworking.common.ANRequest)6