本文整理汇总了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();
}