Search in sources :

Example 1 with User

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

the class GetObjectApiTest method testSynchronousObjectGetRequest.

@SuppressWarnings("unchecked")
public void testSynchronousObjectGetRequest() throws InterruptedException, JSONException {
    server.enqueue(new MockResponse().setBody("{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}"));
    ANRequest request = AndroidNetworking.get(server.url("/").toString()).build();
    ANResponse<User> response = request.executeForObject(User.class);
    assertEquals("Amit", response.getResult().firstName);
    assertEquals("Shekhar", response.getResult().lastName);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ANRequest(com.androidnetworking.common.ANRequest) User(com.androidnetworking.model.User)

Example 2 with User

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

the class GetObjectApiTest method testResponseBodyAndObjectListGet.

public void testResponseBodyAndObjectListGet() 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().getAsOkHttpResponseAndObjectList(User.class, new OkHttpResponseAndParsedRequestListener<List<User>>() {

        @Override
        public void onResponse(Response okHttpResponse, List<User> userList) {
            firstNameRef.set(userList.get(0).firstName);
            lastNameRef.set(userList.get(0).lastName);
            responseBodySuccess.set(okHttpResponse.isSuccessful());
            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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) User(com.androidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch) ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) List(java.util.List)

Example 3 with User

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

the class GetObjectApiTest method testResponseBodyAndObjectGet404.

public void testResponseBodyAndObjectGet404() 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.get(server.url("/").toString()).setExecutor(Executors.newSingleThreadExecutor()).build().getAsOkHttpResponseAndObject(User.class, new OkHttpResponseAndParsedRequestListener<User>() {

        @Override
        public void onResponse(Response okHttpResponse, 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 : ANResponse(com.androidnetworking.common.ANResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) User(com.androidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with User

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

the class GetObjectApiTest method testResponseBodyAndObjectGet.

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

Example 5 with User

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

the class GetObjectApiTest method testObjectListGetRequest404.

public void testObjectListGetRequest404() 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().getAsObjectList(User.class, new ParsedRequestListener<List<User>>() {

        @Override
        public void onResponse(List<User> userList) {
            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.androidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List)

Aggregations

User (com.androidnetworking.model.User)36 MockResponse (okhttp3.mockwebserver.MockResponse)36 ANError (com.androidnetworking.error.ANError)30 CountDownLatch (java.util.concurrent.CountDownLatch)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)27 ANResponse (com.androidnetworking.common.ANResponse)15 List (java.util.List)15 Response (okhttp3.Response)15 ANRequest (com.androidnetworking.common.ANRequest)9