Search in sources :

Example 26 with AnalyticsListener

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

the class OkHttpResponseTestActivity method getResponseOnlyIfCached.

public void getResponseOnlyIfCached(View view) {
    AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0").addQueryParameter("limit", "3").setTag(this).setPriority(Priority.LOW).getResponseOnlyIfCached().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);
        }
    }).getAsOkHttpResponseAndJSONArray(new OkHttpResponseAndJSONArrayRequestListener() {

        @Override
        public void onResponse(Response okHttpResponse, JSONArray 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) JSONArray(org.json.JSONArray) ANError(com.androidnetworking.error.ANError) OkHttpResponseAndJSONArrayRequestListener(com.androidnetworking.interfaces.OkHttpResponseAndJSONArrayRequestListener)

Example 27 with AnalyticsListener

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

the class OkHttpResponseTestActivity method checkSynchronousCall.

public void checkSynchronousCall(View view) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            String url = "http://www.colorado.edu/conflict/peace/download/peace_problem.ZIP";
            ANRequest requestOne = AndroidNetworking.download(url, Utils.getRootDirPath(getApplicationContext()), "file1.zip").setPriority(Priority.HIGH).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);
                }
            }).setDownloadProgressListener(new DownloadProgressListener() {

                @Override
                public void onProgress(long bytesDownloaded, long totalBytes) {
                    Log.d(TAG, "bytesDownloaded : " + bytesDownloaded + " totalBytes : " + totalBytes);
                    Log.d(TAG, "setDownloadProgressListener isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
                }
            });
            ANResponse<String> responseOne = requestOne.executeForDownload();
            if (responseOne.isSuccess()) {
                Log.d(TAG, "checkSynchronousCall : download success");
                Log.d(TAG, "checkSynchronousCall : download result " + responseOne.getResult());
                Response response = responseOne.getOkHttpResponse();
                Log.d(TAG, "checkSynchronousCall : headers : " + response.headers().toString());
            } else {
                Log.d(TAG, "checkSynchronousCall : download failed");
                Utils.logError(TAG, responseOne.getError());
            }
            ANRequest requestTwo = 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);
                }
            });
            ANResponse<List<User>> responseTwo = requestTwo.executeForObjectList(User.class);
            if (responseTwo.isSuccess()) {
                Log.d(TAG, "checkSynchronousCall : response success");
                List<User> users = responseTwo.getResult();
                Log.d(TAG, "userList size : " + users.size());
                for (User user : users) {
                    Log.d(TAG, "id : " + user.id);
                    Log.d(TAG, "firstname : " + user.firstname);
                    Log.d(TAG, "lastname : " + user.lastname);
                }
                Response response = responseTwo.getOkHttpResponse();
                Log.d(TAG, "checkSynchronousCall : headers : " + response.headers().toString());
            } else {
                Log.d(TAG, "checkSynchronousCall : response failed");
                Utils.logError(TAG, responseTwo.getError());
            }
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("firstname", "Rohit");
                jsonObject.put("lastname", "Kumar");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            ANRequest requestThree = AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER).addJSONObjectBody(jsonObject).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);
                }
            });
            ANResponse<JSONObject> responseThree = requestThree.executeForJSONObject();
            if (responseThree.isSuccess()) {
                Log.d(TAG, "checkSynchronousCall : jsonObjectANResponse success");
                JSONObject jsonObjectFinal = responseThree.getResult();
                Log.d(TAG, "checkSynchronousCall : jsonObjectANResponse result " + jsonObjectFinal.toString());
                Response response = responseThree.getOkHttpResponse();
                Log.d(TAG, "checkSynchronousCall : headers : " + response.headers().toString());
            } else {
                Log.d(TAG, "checkSynchronousCall : jsonObjectANResponse failed");
                Utils.logError(TAG, responseThree.getError());
            }
            ANRequest requestFour = 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);
                }
            });
            ANResponse<Response> responseFour = requestFour.executeForOkHttpResponse();
            if (responseFour.isSuccess()) {
                Log.d(TAG, "checkSynchronousCall : okHttpResponse success");
                Response okHttpResponse = responseFour.getResult();
                if (okHttpResponse != null) {
                    if (okHttpResponse.isSuccessful()) {
                        Log.d(TAG, "response is successful");
                        try {
                            Log.d(TAG, "response : " + okHttpResponse.body().source().readUtf8());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        Log.d(TAG, "response is not successful");
                    }
                } else {
                    Log.d(TAG, "response is null");
                }
            } else {
                Log.d(TAG, "checkSynchronousCall : okHttpResponse failed");
                Utils.logError(TAG, responseFour.getError());
            }
        }
    }).start();
}
Also used : ANRequest(com.androidnetworking.common.ANRequest) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) User(com.networking.model.User) DownloadProgressListener(com.androidnetworking.interfaces.DownloadProgressListener) JSONException(org.json.JSONException) IOException(java.io.IOException) Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) JSONObject(org.json.JSONObject) List(java.util.List)

Example 28 with AnalyticsListener

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

the class OkHttpResponseTestActivity method cleanupDestinationTest.

public void cleanupDestinationTest(View view) {
    String url = "http://i.imgur.com/m6K1DCQ.jpg";
    AndroidNetworking.download(url, Utils.getRootDirPath(getApplicationContext()), "cleanupDestinationTest.jpg").setPriority(Priority.HIGH).setTag("cleanupDestinationTest").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);
        }
    }).setDownloadProgressListener(new DownloadProgressListener() {

        @Override
        public void onProgress(long bytesDownloaded, long totalBytes) {
            Log.d(TAG, "bytesDownloaded : " + bytesDownloaded + " totalBytes : " + totalBytes);
            Log.d(TAG, "setDownloadProgressListener isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
            if (bytesDownloaded > 50) {
                AndroidNetworking.cancel("cleanupDestinationTest");
                Log.d(TAG, "cancel: cleanupDestinationTest");
            }
        }
    }).startDownload(new DownloadListener() {

        @Override
        public void onDownloadComplete() {
            Log.d(TAG, "File download Completed");
            Log.d(TAG, "onDownloadComplete isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
        }

        @Override
        public void onError(ANError error) {
            if (error.getErrorCode() != 0) {
                // received ANError from server
                // error.getErrorCode() - the ANError code from server
                // error.getErrorBody() - the ANError body from server
                // error.getErrorDetail() - just an ANError detail
                Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
            } else {
                // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
            }
        }
    });
}
Also used : AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) DownloadProgressListener(com.androidnetworking.interfaces.DownloadProgressListener) DownloadListener(com.androidnetworking.interfaces.DownloadListener) ANError(com.androidnetworking.error.ANError)

Example 29 with AnalyticsListener

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

the class ApiTestActivity 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()));
        }
    }).getAsJSONObject(new JSONObjectRequestListener() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, "Image upload Completed");
            Log.d(TAG, "onResponse object : " + response.toString());
        }

        @Override
        public void onError(ANError error) {
            if (error.getErrorCode() != 0) {
                // received ANError from server
                // error.getErrorCode() - the ANError code from server
                // error.getErrorBody() - the ANError body from server
                // error.getErrorDetail() - just a ANError detail
                Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
            } else {
                // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
            }
        }
    });
}
Also used : UploadProgressListener(com.androidnetworking.interfaces.UploadProgressListener) AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) ANError(com.androidnetworking.error.ANError) File(java.io.File) JSONObjectRequestListener(com.androidnetworking.interfaces.JSONObjectRequestListener)

Example 30 with AnalyticsListener

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

the class ApiTestActivity method doNotCacheResponse.

public void doNotCacheResponse(View view) {
    AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0").addQueryParameter("limit", "3").setTag(this).setPriority(Priority.LOW).doNotCacheResponse().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);
        }
    }).getAsJSONArray(new JSONArrayRequestListener() {

        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, "onResponse array : " + response.toString());
            Log.d(TAG, "onResponse isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
        }

        @Override
        public void onError(ANError error) {
            if (error.getErrorCode() != 0) {
                // received ANError from server
                // error.getErrorCode() - the ANError code from server
                // error.getErrorBody() - the ANError body from server
                // error.getErrorDetail() - just a ANError detail
                Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
            } else {
                // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
            }
        }
    });
}
Also used : AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONArrayRequestListener(com.androidnetworking.interfaces.JSONArrayRequestListener) JSONArray(org.json.JSONArray) ANError(com.androidnetworking.error.ANError)

Aggregations

AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)51 ANError (com.androidnetworking.error.ANError)49 JSONObject (org.json.JSONObject)23 ANResponse (com.androidnetworking.common.ANResponse)15 Response (okhttp3.Response)15 JSONArray (org.json.JSONArray)11 DownloadProgressListener (com.androidnetworking.interfaces.DownloadProgressListener)10 DownloadListener (com.androidnetworking.interfaces.DownloadListener)8 JSONObjectRequestListener (com.androidnetworking.interfaces.JSONObjectRequestListener)7 Disposable (io.reactivex.disposables.Disposable)7 ANRequest (com.androidnetworking.common.ANRequest)6 JSONArrayRequestListener (com.androidnetworking.interfaces.JSONArrayRequestListener)6 OkHttpResponseAndJSONObjectRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndJSONObjectRequestListener)6 UploadProgressListener (com.androidnetworking.interfaces.UploadProgressListener)6 File (java.io.File)6 JSONException (org.json.JSONException)6 OkHttpResponseAndJSONArrayRequestListener (com.androidnetworking.interfaces.OkHttpResponseAndJSONArrayRequestListener)5 IOException (java.io.IOException)4 OkHttpClient (okhttp3.OkHttpClient)4 Bitmap (android.graphics.Bitmap)3