Java Code Examples for retrofit.client.Response#getHeaders()

The following examples show how to use retrofit.client.Response#getHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiUtils.java    From GithubContributorsLib with Apache License 2.0 6 votes vote down vote up
public static int getNextPage(Response r) {
    List<Header> headers = r.getHeaders();
    Map<String, String> headersMap = new HashMap<String, String>(headers.size());
    for (Header header : headers) {
        headersMap.put(header.getName(), header.getValue());
    }

    String link = headersMap.get("Link");

    if (link != null) {
        String[] parts = link.split(",");
        try {
            PaginationLink bottomPaginationLink = new PaginationLink(parts[0]);
            if (bottomPaginationLink.rel == RelType.next) {
                return bottomPaginationLink.page;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return -1;
}
 
Example 2
Source File: SnippetDetailFragment.java    From Android-REST-API-Explorer with MIT License 5 votes vote down vote up
private void maybeDisplayResponseHeaders(Response response) {
    if (null != response.getHeaders()) {
        List<Header> headers = response.getHeaders();
        String headerText = "";
        for (Header header : headers) {
            headerText += header.getName() + " : " + header.getValue() + "\n";
        }
        mResponseHeaders.setText(headerText);
    }
}
 
Example 3
Source File: MoverAuthenticator.java    From Mover with Apache License 2.0 5 votes vote down vote up
@Override
public Bundle getAuthToken(AccountManager manager, Account account) {
    if(!mSigning){
        mPassword = manager.getPassword(account);
    }

    Bundle result = new Bundle();
    try {
        Response response = mService.signIn(account.name, mPassword);

        if (isResponseSuccess(response)) {
            JSONObject json = asJson(asString(response));

            if (json == null || json.has("errors") || json.has("error")) {
                result.putInt(KEY_ERROR_CODE, ERROR_CODE_INVALID_USER_DATA);
                return result;
            }

            for (Header header : response.getHeaders()) {
                if (isCookiesHeader(header)) {
                    String cookies = header.getValue();
                    String token = findToken(HttpCookie.parse(cookies));

                    if (token != null) {
                        result.putString(KEY_AUTHTOKEN, token);
                        result.putString(USER_PICTURE_URL, findUserImage(mService.channel(account.name)));
                    }
                }
            }
        } else {
            result.putInt(KEY_ERROR_CODE, ERROR_CODE_NETWORK_ERROR);
        }
    }catch (RetrofitError error){
        result.putInt(KEY_ERROR_CODE, ERROR_CODE_NETWORK_ERROR);
    }

    return result;
}
 
Example 4
Source File: RestAdapter.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
/**
 * Log response headers and body. Consumes response body and returns identical replacement.
 */
private Response logAndReplaceResponse(String url, Response response, long elapsedTime) throws IOException {
    log.log(String.format("<--- HTTP %s %s (%sms)", response.getStatus(), url, elapsedTime));

    if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal()) {
        for (Header header : response.getHeaders()) {
            log.log(header.getName() + ": " + header.getValue());
        }

        long bodySize = 0;
        TypedInput body = response.getBody();
        if (body != null) {
            bodySize = body.length();

            if (logLevel.ordinal() >= LogLevel.FULL.ordinal()) {
                if (!response.getHeaders().isEmpty()) {
                    log.log("");
                }

                if (!(body instanceof TypedByteArray)) {
                    // Read the entire response body to we can log it and replace the original response
                    response = Utils.readBodyToBytesIfNecessary(response);
                    body = response.getBody();
                }

                byte[] bodyBytes = ((TypedByteArray) body).getBytes();
                bodySize = bodyBytes.length;
                String bodyMime = body.mimeType();
                String bodyCharset = MimeUtil.parseCharset(bodyMime);
                String bodyString = new String(bodyBytes, bodyCharset);
                for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
                    int end = Math.min(len, i + LOG_CHUNK_SIZE);
                    log.log(bodyString.substring(i, end));
                }
            }
        }

        log.log(String.format("<--- END HTTP (%s-byte body)", bodySize));
    }

    return response;
}
 
Example 5
Source File: Utils.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
static Response replaceResponseBody(Response response, TypedInput body) {
    return new Response(response.getStatus(), response.getReason(), response.getHeaders(), body);
}