本文整理匯總了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);
}
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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;
}
示例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();
}
}
}
示例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();
}