我知道最好使用基于 HttpUrlConnection 的 OKHttp,但我不知道为什么。
为什么使用 OKHttp 而不是实现的 HTTPUrlConnection 更好?
而且..如何发送混合数据。POST 变量和文件。最好是如果有多线程可能。
这是我的客户端使用 HttpUrlConnection 的示例。在 OKHTTP 中会是什么样子?
Dataclass 和 DataPackage 只是用于保存文件和数据以进行发送的模式。
客户端 if self 将通过使用 FactoryManager (LazyLoading) 调用
public static DefaultHttpPostClient getEnterpriseHttpManager() {
if (defaultHttpPostClient == null) {
defaultHttpPostClient = new DefaultHttpPostClient();
}
return defaultHttpPostClient;
}
每次调用时,我的 HttpClient 的构造函数都会请求它的设置。
public static HttpConnectionSettings getDefaultHttpConnectionSettings() throws MalformedURLException {
HttpConnectionSettings httpConnectionSettings = new HttpConnectionSettings();
httpConnectionSettings.setPost(true);
httpConnectionSettings.setMultiThreaded(true);
httpConnectionSettings.setUrl(new URL(MYURL);
return httpConnectionSettings;
}
public HttpResult executeRequest() {
HttpURLConnection connection = null;
OutputStream out = null;
InputStream in = null;
HttpResult httpResult = new HttpResult();
try {
connection = Mfactory.getOkHttpClient().open(httpConnectionSettings.getUrl());
connection.setConnectTimeout(300000);
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod((httpConnectionSettings.isPost()) ? "POST" : "GET");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setReadTimeout(300000);
connection.setDefaultUseCaches(true);
connection.setRequestProperty("Accept-Charset", "UTF-8");
if (httpConnectionSettings.isPost()) {
connection.setRequestProperty("Content-Type", "multipart/form-data, boundary=XXXXXXXXXXYY");
}
out = connection.getOutputStream();
MultipartEntity multipartEntity = new MultipartEntity();
for (Object aMData : dataClass.getData().entrySet()) {
Map.Entry mapEntry = (Map.Entry) aMData;
String keyValue = (String) mapEntry.getKey();
String value = (String) mapEntry.getValue();
if (value != null) {
multipartEntity.addPart(new StringPart(keyValue, value));
} else {
Log.d(this.getClass().getSimpleName(), "Value was null for key " + keyValue);
}
}
for (DataPackage dataPackage : dataClass.getDataPackages()) {
if (dataPackage.getbFile() != null) {
multipartEntity.addPart(new BytePart("upfile", dataPackage.getbFile(), dataPackage.getfName()));
} else if (!dataPackage.getsFile().equals("")) {
multipartEntity.addPart(new FilePart(new File(dataPackage.getsFile()), dataPackage.getfName(), dataPackage.getMimetype()));
} else if (dataPackage.getmFile() != null) {
multipartEntity.addPart(new FilePart(dataPackage.getmFile(), dataPackage.getfName(), dataPackage.getMimetype()));
}
if (dataPackage.getMimetype() != null) {
multipartEntity.addPart(new StringPart("mime", dataPackage.getMimetype()));
}
if (dataPackage.getFilesize() != 0) {
multipartEntity.addPart(new StringPart("filesize_orig", String.valueOf(dataPackage.getFilesize())));
}
if (dataPackage.getFilepath() != null) {
multipartEntity.addPart(new StringPart("filepath_orig", dataPackage.getFilepath()));
}
if (dataPackage.getfName() != null) {
multipartEntity.addPart(new StringPart("filename_orig", dataPackage.getfName()));
}
if (dataPackage.getLastmodified() != 0) {
multipartEntity.addPart(new StringPart("lastmodified_orig", String.valueOf(dataPackage.getLastmodified())));
}
if (dataPackage.getAdded() != 0) {
multipartEntity.addPart(new StringPart("added_orig", String.valueOf(dataPackage.getAdded())));
}
}
multipartEntity.writeTo(out);
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.e("httpResult", "Response: " + connection.getResponseCode() + " " + connection.getResponseMessage() + " content:" + connection.getURL());
httpResult.setSuccess(false);
} else {
Log.d("httpResult", "Response: " + connection.getResponseCode() + " " + connection.getResponseMessage() + " content:" + connection.getURL());
httpResult.setSuccess(true);
httpResult.setInputStream(connection.getInputStream());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
for (DataPackage dataPackage : dataClass.getDataPackages()) {
if (dataPackage.isDeleteAfter()) {
new File(dataPackage.getFilepath()).delete();
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
}
}
if (in != null) {
try {
in.close();
} catch (Exception e) {
}
}
return httpResult;
}
}
切换到 OKHttp 有意义吗?我有很多要求。有时大约有 100 个线程打开(每个带有 Executor 的线程都是它自己的 HttpRequest)。
我为什么要使用 OKHttp?感谢到目前为止。