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