本文整理汇总了Java中com.squareup.okhttp.Response.header方法的典型用法代码示例。如果您正苦于以下问题:Java Response.header方法的具体用法?Java Response.header怎么用?Java Response.header使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.okhttp.Response
的用法示例。
在下文中一共展示了Response.header方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: prepareDownloadFile
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
/**
* Prepare file for download
*
* @param response An instance of the Response object
* @throws IOException If fail to prepare file for download
* @return Prepared file for the download
*/
public File prepareDownloadFile(Response response) throws IOException {
String filename = null;
String contentDisposition = response.header("Content-Disposition");
if (contentDisposition != null && !"".equals(contentDisposition)) {
// Get filename from the Content-Disposition header.
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
Matcher matcher = pattern.matcher(contentDisposition);
if (matcher.find()) {
filename = sanitizeFilename(matcher.group(1));
}
}
String prefix = null;
String suffix = null;
if (filename == null) {
prefix = "download-";
suffix = "";
} else {
int pos = filename.lastIndexOf(".");
if (pos == -1) {
prefix = filename + "-";
} else {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}
if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
}
示例2: toWebResource
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
protected WebResourceResponse toWebResource(Response response) throws IOException {
if (response == null || !response.isSuccessful()) {
return null;
}
String mimeType = response.header("Content-Type");
String charset = response.header("charset");
return new WebResourceResponse(mimeType, charset, response.body().byteStream());
}
示例3: isCacheable
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
public static boolean isCacheable(Response response, Request request) {
switch (response.code()) {
case 200:
case 203:
case 204:
case 300:
case SampleTinkerReport.KEY_LOADED_MISMATCH_LIB /*301*/:
case 308:
case SampleTinkerReport.KEY_LOADED_SUCC_COST_OTHER /*404*/:
case 405:
case 410:
case 414:
case 501:
break;
case SampleTinkerReport.KEY_LOADED_MISMATCH_RESOURCE /*302*/:
case 307:
if (response.header("Expires") == null && response.cacheControl().maxAgeSeconds()
== -1 && !response.cacheControl().isPublic() && !response.cacheControl()
.isPrivate()) {
return false;
}
default:
return false;
}
return (response.cacheControl().noStore() || request.cacheControl().noStore()) ? false :
true;
}
示例4: onRet
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
private void onRet(Response response, String ip, long duration, CompletionHandler complete) {
int code = response.code();
String reqId = response.header("X-Reqid");
reqId = reqId == null ? null : reqId.trim();
byte[] body = null;
String error = null;
try {
body = response.body().bytes();
} catch (IOException e) {
error = e.getMessage();
}
JSONObject json = null;
if (!ctype(response).equals("application/json") || body == null) {
String str = new String(body);
} else {
try {
json = buildJsonResp(body);
if (response.code() != 200) {
error = json.optString("error", new String(body, Constants.UTF_8));
}
} catch (Exception e2) {
if (response.code() < 300) {
error = e2.getMessage();
}
}
}
URL u = response.request().url();
final ResponseInfo info = new ResponseInfo(code, reqId, response.header("X-Log"), via
(response), u.getHost(), u.getPath(), ip, u.getPort(), (double) duration, 0, error);
final CompletionHandler completionHandler = complete;
final JSONObject jSONObject = json;
AsyncRun.run(new Runnable() {
public void run() {
completionHandler.complete(info, jSONObject);
}
});
}
示例5: onOpen
import com.squareup.okhttp.Response; //导入方法依赖的package包/类
@Override
public void onOpen(final WebSocket webSocket, Response response) {
String protocol = response.header(STREAM_PROTOCOL_HEADER, "missing");
listener.open(protocol, webSocket);
}