Search in sources :

Example 1 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class OkHttpResponseTestActivity method createAnUser.

public void createAnUser(View view) {
    AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER).addBodyParameter("firstname", "Suman").addBodyParameter("lastname", "Shekhar").setTag(this).setPriority(Priority.LOW).build().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);
        }
    }).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);
        }
    });
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) ANError(com.androidnetworking.error.ANError) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Example 2 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class OkHttpResponseTestActivity method uploadImage.

public void uploadImage(final View view) {
    AndroidNetworking.upload(ApiEndPoint.BASE_URL + ApiEndPoint.UPLOAD_IMAGE).setPriority(Priority.MEDIUM).addMultipartFile("image", new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "test.png")).setTag(this).build().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);
        }
    }).setUploadProgressListener(new UploadProgressListener() {

        @Override
        public void onProgress(long bytesUploaded, long totalBytes) {
            Log.d(TAG, "bytesUploaded : " + bytesUploaded + " totalBytes : " + totalBytes);
            Log.d(TAG, "setUploadProgressListener isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
        }
    }).getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, JSONObject response) {
            Log.d(TAG, "Image upload Completed");
            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);
        }
    });
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) UploadProgressListener(com.androidnetworking.interfaces.UploadProgressListener) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) ANError(com.androidnetworking.error.ANError) File(java.io.File) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Example 3 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class MultipartJSONApiTest method testResponseBodyAndJSONObjectMultipart.

public void testResponseBodyAndJSONObjectMultipart() 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.upload(server.url("/").toString()).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());
                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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) JSONException(org.json.JSONException) 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) JSONObject(org.json.JSONObject) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Example 4 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class PostJSONApiTest method testResponseBodyAndJSONObjectPost404.

public void testResponseBodyAndJSONObjectPost404() 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.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) {
            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) JSONObject(org.json.JSONObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Example 5 with OkHttpResponseAndJSONObjectRequestListener

use of com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener in project Fast-Android-Networking by amitshekhariitbhu.

the class GetJSONApiTest method testResponseBodyAndJSONObjectGet.

public void testResponseBodyAndJSONObjectGet() 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().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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) JSONException(org.json.JSONException) 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) JSONObject(org.json.JSONObject) OkHttpResponseAndJSONObjectRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)

Aggregations

ANResponse (com.androidnetworking.common.ANResponse)15 ANError (com.androidnetworking.error.ANError)15 OkHttpResponseAndJSONObjectRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)15 Response (okhttp3.Response)15 JSONObject (org.json.JSONObject)15 CountDownLatch (java.util.concurrent.CountDownLatch)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 MockResponse (okhttp3.mockwebserver.MockResponse)9 JSONException (org.json.JSONException)7 AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)6 ANRequest (com.androidnetworking.common.ANRequest)2 UploadProgressListener (com.androidnetworking.interfaces.UploadProgressListener)1 File (java.io.File)1 OkHttpClient (okhttp3.OkHttpClient)1