Search in sources :

Example 1 with SampleJSON

use of com.loopj.android.http.sample.util.SampleJSON in project android-async-http by loopj.

the class CustomCASample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new BaseJsonHttpResponseHandler<SampleJSON>() {

        @Override
        public void onStart() {
            clearOutputs();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (response != null) {
                debugResponse(LOG_TAG, rawJsonResponse);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, throwable);
            if (errorResponse != null) {
                debugResponse(LOG_TAG, rawJsonData);
            }
        }

        @Override
        protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
            return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
        }
    };
}
Also used : SampleJSON(com.loopj.android.http.sample.util.SampleJSON) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseJsonHttpResponseHandler(com.loopj.android.http.BaseJsonHttpResponseHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with SampleJSON

use of com.loopj.android.http.sample.util.SampleJSON in project android-async-http by loopj.

the class Http401AuthSample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new BaseJsonHttpResponseHandler<SampleJSON>() {

        @Override
        public void onStart() {
            clearOutputs();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (response != null) {
                debugResponse(LOG_TAG, rawJsonResponse);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, throwable);
            // Ask the user for credentials if required by the server.
            if (statusCode == 401) {
                String realm = "Protected Page";
                String authType = null;
                // Cycle through the headers and look for the WWW-Authenticate header.
                for (Header header : headers) {
                    String headerName = header.getName();
                    if (HEADER_WWW_AUTHENTICATE.equalsIgnoreCase(headerName)) {
                        String headerValue = header.getValue().trim();
                        String headerValueLowerCase = headerValue.toLowerCase(Locale.US);
                        // Get the type of auth requested.
                        int charPos = headerValueLowerCase.indexOf(' ');
                        if (0 < charPos) {
                            authType = headerValueLowerCase.substring(0, charPos);
                            // The second part should begin with a "realm=" prefix.
                            if (headerValueLowerCase.substring(1 + charPos).startsWith(HEADER_REALM_PREFIX)) {
                                // The new realm value, including any possible wrapping quotation.
                                realm = headerValue.substring(1 + charPos + HEADER_REALM_PREFIX.length());
                                // If realm starts with a quote, remove surrounding quotes.
                                if (realm.charAt(0) == '"' || realm.charAt(0) == '\'') {
                                    realm = realm.substring(1, realm.length() - 1);
                                }
                            }
                        }
                    }
                }
                // We will support basic auth in this sample.
                if (authType != null && HEADER_BASIC.equals(authType)) {
                    // Show a dialog for the user and request user/pass.
                    Log.d(LOG_TAG, HEADER_REALM_PREFIX + realm);
                    // Present the dialog.
                    postRunnable(new DialogRunnable(realm));
                }
            }
        }

        @Override
        protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
            return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
        }
    };
}
Also used : Header(cz.msebera.android.httpclient.Header) BasicHeader(cz.msebera.android.httpclient.message.BasicHeader) SampleJSON(com.loopj.android.http.sample.util.SampleJSON) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseJsonHttpResponseHandler(com.loopj.android.http.BaseJsonHttpResponseHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with SampleJSON

use of com.loopj.android.http.sample.util.SampleJSON in project android-async-http by loopj.

the class JsonSample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new BaseJsonHttpResponseHandler<SampleJSON>() {

        @Override
        public void onStart() {
            clearOutputs();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (response != null) {
                debugResponse(LOG_TAG, rawJsonResponse);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, throwable);
            if (errorResponse != null) {
                debugResponse(LOG_TAG, rawJsonData);
            }
        }

        @Override
        protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
            return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
        }
    };
}
Also used : SampleJSON(com.loopj.android.http.sample.util.SampleJSON) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseJsonHttpResponseHandler(com.loopj.android.http.BaseJsonHttpResponseHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with SampleJSON

use of com.loopj.android.http.sample.util.SampleJSON in project android-async-http by loopj.

the class PersistentCookiesSample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new BaseJsonHttpResponseHandler<SampleJSON>() {

        @Override
        public void onStart() {
            clearOutputs();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (response != null) {
                debugResponse(LOG_TAG, rawJsonResponse);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, throwable);
            if (errorResponse != null) {
                debugResponse(LOG_TAG, rawJsonData);
            }
        }

        @Override
        protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
            return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
        }
    };
}
Also used : SampleJSON(com.loopj.android.http.sample.util.SampleJSON) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseJsonHttpResponseHandler(com.loopj.android.http.BaseJsonHttpResponseHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 BaseJsonHttpResponseHandler (com.loopj.android.http.BaseJsonHttpResponseHandler)4 SampleJSON (com.loopj.android.http.sample.util.SampleJSON)4 Header (cz.msebera.android.httpclient.Header)1 BasicHeader (cz.msebera.android.httpclient.message.BasicHeader)1