Search in sources :

Example 6 with UploadProgressListener

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

the class OkHttpResponseTestActivity method checkOkHttpResponse.

public void checkOkHttpResponse(View view) {
    AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0").addQueryParameter("limit", "3").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);
        }
    }).getAsOkHttpResponse(new OkHttpResponseListener() {

        @Override
        public void onResponse(Response response) {
            if (response != null) {
                if (response.isSuccessful()) {
                    Log.d(TAG, "response is successful");
                    try {
                        Log.d(TAG, "response : " + response.body().source().readUtf8());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.d(TAG, "response is not successful");
                }
            } else {
                Log.d(TAG, "response is null");
            }
        }

        @Override
        public void onError(ANError anError) {
            Utils.logError(TAG, anError);
        }
    });
    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);
        }
    }).getAsOkHttpResponse(new OkHttpResponseListener() {

        @Override
        public void onResponse(Response response) {
            if (response != null) {
                if (response.isSuccessful()) {
                    Log.d(TAG, "response is successful");
                    try {
                        Log.d(TAG, "response : " + response.body().source().readUtf8());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.d(TAG, "response is not successful");
                }
            } else {
                Log.d(TAG, "response is null");
            }
        }

        @Override
        public void onError(ANError anError) {
            Utils.logError(TAG, anError);
        }
    });
    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()));
        }
    }).getAsOkHttpResponse(new OkHttpResponseListener() {

        @Override
        public void onResponse(Response response) {
            if (response != null) {
                if (response.isSuccessful()) {
                    Log.d(TAG, "response is successful");
                    try {
                        Log.d(TAG, "response : " + response.body().source().readUtf8());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.d(TAG, "response is not successful");
                }
            } else {
                Log.d(TAG, "response is null");
            }
        }

        @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) IOException(java.io.IOException) ANError(com.androidnetworking.error.ANError) File(java.io.File) OkHttpResponseListener(com.androidnetworking.interfaces.OkHttpResponseListener)

Aggregations

ANError (com.androidnetworking.error.ANError)6 AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)6 UploadProgressListener (com.androidnetworking.interfaces.UploadProgressListener)6 File (java.io.File)6 JSONObject (org.json.JSONObject)4 ANResponse (com.androidnetworking.common.ANResponse)3 Response (okhttp3.Response)3 OkHttpResponseListener (com.androidnetworking.interfaces.OkHttpResponseListener)2 IOException (java.io.IOException)2 JSONObjectRequestListener (com.androidnetworking.interfaces.JSONObjectRequestListener)1 OkHttpResponseAndJSONObjectRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)1 Disposable (io.reactivex.disposables.Disposable)1