android 上传图片 java.net.SocketException: Socket closed

牛大炮 2016-01-08 03:18:57
百度了很久,也没解决哎
代码如下
求大神Help
 public static InputStream image_post(String url, List<NameValuePair> params) throws AppException {
InputStream is = null;
String result = "";
int time = 0;
do {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
//设置图片读取超时时间
//httpclient.getParams().setParameter("http.socket.timeout", new Integer(30000));
HttpPost httppost = new HttpPost(url);
MultipartEntity mpEntity = new MultipartEntity();
for (int index = 0; index < params.size(); index++) {
if (params.get(index).getName().equals("filePath")) {
String filePath = new String(params.get(index).getValue());
String mimeType = "image/jpeg";
String extension = filePath.substring(filePath.lastIndexOf(".") + 1,
filePath.length());
File file = new File(filePath);
if (null != extension && !"".equals(extension)) {
if (extension.equals("jpg")) {
mimeType = "image/jpeg";
} else if (extension.equals("png")) {
mimeType = "image/png";
} else if (extension.equals("gif")) {
mimeType = "image/gif";
}
}
ContentBody cbFile = new FileBody(file, mimeType);
mpEntity.addPart("imgFile", cbFile);
} else {
mpEntity.addPart(params.get(index).getName(), new StringBody(params.get(index).getValue()));
}
}
httppost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httppost);
is = response.getEntity().getContent();
httpclient.getConnectionManager().shutdown();
break;
} catch (HttpException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
throw AppException.http(e);
} catch (IOException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
throw AppException.network(e);
}
} while (time < RETRY_TIME);

return is;

仅剩的10分 奉献上了~
...全文
208 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧