60。httpClient上传图片文件的功能:
public void sendHead(final File file,final String url){
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient(); //httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000); //设置请求超时 //httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);//设置连接超时
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
if(file != null)
entity.addPart("file", new FileBody(file));
entity.addPart("uid", new StringBody(serverSession.personinfo.getUid()));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode() == 200)
{
HttpEntity sentity = response.getEntity();
String content = EntityUtils.toString(sentity);
try {
if(content != null)
{
JSONObject obj = new JSONObject(content);
if(obj.getString("flag").equals("succ"))
{
headUrl = obj.getString("picUrl");
handler.sendEmptyMessage(0 * 123);
}else{
new PersoncenterToast(PersonInfoEditActivity.this).showToast("修改头像失败");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
评论2
最新资源