Search in sources :

Example 1 with RxANRequest

use of com.rxandroidnetworking.RxANRequest in project Fast-Android-Networking by amitshekhariitbhu.

the class RxApiTestActivity method checkForHeaderPost.

public void checkForHeaderPost(View view) {
    RxANRequest.PostRequestBuilder postRequestBuilder = RxAndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.CHECK_FOR_HEADER);
    postRequestBuilder.addHeaders("token", "1234");
    RxANRequest rxAnRequest = postRequestBuilder.setTag(this).build();
    rxAnRequest.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);
        }
    });
    rxAnRequest.getJSONObjectObservable().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<JSONObject>() {

        @Override
        public void onCompleted() {
            Log.d(TAG, "onComplete Detail : checkForHeaderPost completed");
        }

        @Override
        public void onError(Throwable e) {
            if (e instanceof ANError) {
                ANError anError = (ANError) e;
                if (anError.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 : " + anError.getErrorCode());
                    Log.d(TAG, "onError errorBody : " + anError.getErrorBody());
                    Log.d(TAG, "onError errorDetail : " + anError.getErrorDetail());
                } else {
                    // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                    Log.d(TAG, "onError errorDetail : " + anError.getErrorDetail());
                }
            } else {
                Log.d(TAG, "onError errorMessage : " + e.getMessage());
            }
        }

        @Override
        public void onNext(JSONObject response) {
            Log.d(TAG, "onResponse object : " + response.toString());
            Log.d(TAG, "onResponse isMainThread : " + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
        }
    });
}
Also used : AnalyticsListener(com.androidnetworking.interfaces.AnalyticsListener) JSONObject(org.json.JSONObject) ANError(com.androidnetworking.error.ANError) RxANRequest(com.rxandroidnetworking.RxANRequest)

Aggregations

ANError (com.androidnetworking.error.ANError)1 AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)1 RxANRequest (com.rxandroidnetworking.RxANRequest)1 JSONObject (org.json.JSONObject)1