當前位置: 首頁>>代碼示例>>Java>>正文


Java Response.body方法代碼示例

本文整理匯總了Java中com.squareup.okhttp.Response.body方法的典型用法代碼示例。如果您正苦於以下問題:Java Response.body方法的具體用法?Java Response.body怎麽用?Java Response.body使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.squareup.okhttp.Response的用法示例。


在下文中一共展示了Response.body方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: handleResponse

import com.squareup.okhttp.Response; //導入方法依賴的package包/類
/**
 * Handle the given response, return the deserialized object when the response is successful.
 *
 * @param <T> Type
 * @param response Response
 * @param returnType Return type
 * @throws ApiException If the response has a unsuccessful status code or
 *   fail to deserialize the response body
 * @return Type
 */
public <T> T handleResponse(Response response, Type returnType) throws ApiException {
    if (response.isSuccessful()) {
        if (returnType == null || response.code() == 204) {
            // returning null if the returnType is not defined,
            // or the status code is 204 (No Content)
            return null;
        } else {
            return deserialize(response, returnType);
        }
    } else {
        String respBody = null;
        if (response.body() != null) {
            try {
                respBody = response.body().string();
            } catch (IOException e) {
                throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap());
            }
        }
        throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody);
    }
}
 
開發者ID:ina-foss,項目名稱:afp-api-client,代碼行數:32,代碼來源:ApiClient.java

示例2: doInBackground

import com.squareup.okhttp.Response; //導入方法依賴的package包/類
/**
 *
 * @param params
 * @return
 */
@Override
protected String doInBackground(Void... params) {
    OkHttpClient client = new OkHttpClient();

    HttpUrl httpUrl = HttpUrl.parse(COIN_DESK_API_URL);
    //System.out.println("Requesting : " + httpUrl.toString());

    FormEncodingBuilder formBody = new FormEncodingBuilder();
    formBody.add("lastHours", "24");
    formBody.add("maxRespArrSize", "24");

    Request request = new Request.Builder()
            .url(httpUrl)
            .post(formBody.build())
            .build();

    String content = null;
    try {
        Response response = client.newCall(request).execute();
        ResponseBody body = response.body();

        if (isZipped(response)) {
            content = unzip(body);
        } else {
            content = body.string();
        }

        body.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return content;
}
 
開發者ID:ehanoc,項目名稱:xwallet,代碼行數:40,代碼來源:CexChartAPI.java

示例3: entityFromOkHttpResponse

import com.squareup.okhttp.Response; //導入方法依賴的package包/類
private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
開發者ID:wangzhaosheng,項目名稱:publicProject,代碼行數:14,代碼來源:OkHttpStack.java

示例4: unzip

import com.squareup.okhttp.Response; //導入方法依賴的package包/類
private Response unzip(Response response) throws IOException {
    if (!this.transparentGzip || !AsyncHttpClient.ENCODING_GZIP.equalsIgnoreCase(this
            .userResponse.header(AsyncHttpClient.HEADER_CONTENT_ENCODING)) || response.body()
            == null) {
        return response;
    }
    GzipSource responseBody = new GzipSource(response.body().source());
    Headers strippedHeaders = response.headers().newBuilder().removeAll(AsyncHttpClient
            .HEADER_CONTENT_ENCODING).removeAll("Content-Length").build();
    return response.newBuilder().headers(strippedHeaders).body(new RealResponseBody
            (strippedHeaders, Okio.buffer(responseBody))).build();
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:13,代碼來源:HttpEngine.java

示例5: doInBackground

import com.squareup.okhttp.Response; //導入方法依賴的package包/類
/**
 *
 * @param params
 * @return
 */
@Override
protected String doInBackground(Void... params) {

    OkHttpClient client = new OkHttpClient();

    HttpUrl httpUrl = HttpUrl.parse(TwitterSearchURL);
    httpUrl = httpUrl.newBuilder()
            .addQueryParameter("q", _searchTerm)
            .addQueryParameter("lang", "en")
            .addQueryParameter("result_type", "recent")
            .build();

    Request request = new Request.Builder()
            .url(httpUrl)
            .addHeader("Authorization", "Bearer " + _bearerToken)
            .addHeader("Accept-Encoding", "gzip")
            .build();

    String content = null;
    try {
        Response response = client.newCall(request).execute();
        ResponseBody body = response.body();

        if (isZipped(response)) {
            content = unzip(body);
        } else {
            content = body.string();
        }

        body.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return content;
}
 
開發者ID:ehanoc,項目名稱:xwallet,代碼行數:42,代碼來源:TwitterSearchApi.java

示例6: intercept

import com.squareup.okhttp.Response; //導入方法依賴的package包/類
@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Response response = null;
    try {
        Log.i(TAG, "request is: \n" + request.toString());
        Log.i(TAG, "request headers are: \n" + request.headers().toString());
        Buffer buffer = new Buffer();
        if (request.body() != null) {
            request.body().writeTo(buffer);
        }
        String bodyStr = buffer.readUtf8();
        Log.i(TAG, "REQUEST body is: \n" + bodyStr);
        response = chain.proceed(request);
        String responseBodyString = "";
        MediaType type = null;
        if (response.body() != null) {
            type = response.body().contentType();
            responseBodyString = response.body().string();
        }
        response = response.newBuilder().body(ResponseBody.create(type, responseBodyString.getBytes())).build();
        Log.i(TAG, "RESPONSE body is \n" + responseBodyString);
        return response;
    } catch (Exception e) {
        Log.e(TAG, "RequestInterceptor: intercept", e);
    }
    return response;
}
 
開發者ID:WorldBank-Transport,項目名稱:RoadLab-Pro,代碼行數:29,代碼來源:RestClient.java

示例7: requestNewToken

import com.squareup.okhttp.Response; //導入方法依賴的package包/類
private void requestNewToken(String grantType) throws IOException {
	Map<String, String> localVarHeaderParams = new HashMap<>();
	localVarHeaderParams.put("Accept", "application/json");

	String method = "GET";

	String[] localVarAuthNames = new String[] {};

	List<Pair> localVarQueryParams = new ArrayList<>();
	Map<String, Object> formParams = new HashMap<>();

	localVarQueryParams.addAll(apiClient.parameterToPairs("", "grant_type", grantType));

	if ("refresh_token".equals(grantType) || "password".equals(grantType)) {
		String client = authenticationProperties.get(KEY_CLIENT);
		String secret = authenticationProperties.get(KEY_SECRET);
		byte[] creds = (client + ":" + secret).getBytes();
		localVarHeaderParams.put("Authorization", "Basic " + Base64.getEncoder().encodeToString(creds));
	}

	if ("refresh_token".equals(grantType)) {
		localVarQueryParams.addAll(apiClient.parameterToPairs("", "refresh_token", currentToken.getRefreshToken()));
	}

	if ("password".equals(grantType)) {
		localVarHeaderParams.put("Content-Type", "multipart/form-data");

		method = "POST";
		formParams.put("username", authenticationProperties.get(KEY_USERNAME));
		formParams.put("password", authenticationProperties.get(KEY_PASSWORD));
	}
	Response response = null;
	try {
		Request r = apiClient.buildRequest("/oauth/token", method, localVarQueryParams, null, localVarHeaderParams, formParams, localVarAuthNames, null);
		logger.debug(r.toString());
		Call call = apiClient.buildCall(r);
		response = call.execute();
		if (response.isSuccessful()) {
			MyOAuthToken newToken = apiClient.deserialize(response, myOAuthTokenType);
			newToken.computeExpirationDate();

			currentToken = newToken;
		} else {
			throw new IOException("Unable to get an authentication token (" + response.code() + ") : Msg(" + response.message() + ") Body("
					+ (response.body() != null ? response.body().string() : "") + ")");
		}
	} catch (ApiException e) {
		throw new IOException(e);
	} finally {
		if (response != null) {
			response.body().close();
		}
	}

}
 
開發者ID:ina-foss,項目名稱:afp-api-client,代碼行數:56,代碼來源:AFPAuthenticationManager.java

示例8: stripBody

import com.squareup.okhttp.Response; //導入方法依賴的package包/類
private static Response stripBody(Response response) {
    return (response == null || response.body() == null) ? response : response.newBuilder()
            .body(null).build();
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:5,代碼來源:HttpEngine.java


注:本文中的com.squareup.okhttp.Response.body方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。