大家好:
我想实现新浪微博Android客户端的发送图片微博的功能。POST中使用httpclient的。相关代码如下:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url); List<BasicNameValuePair> result = new ArrayList<BasicNameValuePair>();
//添加参数
BasicNameValuePair pair = new BasicNameValuePair("status", “我的微博”);
result.add(pair); httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost); 已经能发一条微博消息。但是在发送图片微博时遇到了问题。假设文件形式为:
File file = new File("/sdcard/picture.jpg");
请问版主,如何将该图片添加到请求参数中?另外“采用multipart/form-data编码方式”是什么意思?
BasicNameValuePair pair = new BasicNameValuePair("Content-type", “multipart/form-data”);
result.add(pair);像这样添加参数吗?
恳求各位高手不吝赐教。谢谢
http://blog.csdn.net/z104207/article/details/6634675
关上传图片 主要看communication02()这个方法
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
http://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pro
File file = new File("c:/TRASH/zaba_1.jpg"); MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("userfile", cbFile); httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine());
DefaultOAuthConsumer httpOAuthConsumer = new DefaultOAuthConsumer(OAuthInfo.consumerKey_Sina,OAuthInfo.consumerSecret_Sina);
httpOAuthConsumer.setTokenWithSecret(token,tokenSecret);
String result = null;
try {
URL url = new URL(urlPath);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setDoOutput(true);
request.setRequestMethod("POST");
HttpParameters para = new HttpParameters();
para.put("status", URLEncoder.encode(status,"utf-8").replaceAll("\\+", "%20"));
String boundary = "---------------------------37531613912423";
String content = "--"+boundary+ "\nContent-Disposition: form-data; name=\"status\"\n\n";
String pic = "\n--"+boundary+"\nContent-Disposition: form-data; name=\"pic\"; filename=\"image.jpg\"\nContent-Type: image/jpeg\n\n";
byte[] end_data = ("\n--" + boundary + "--\n").getBytes();
FileInputStream stream = new FileInputStream(aFile);
byte[] file = new byte[(int) aFile.length()];
stream.read(file);
request.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary); //设置表单类型和分隔符
request.setRequestProperty("Content-Length", String.valueOf(content.getBytes().length + status.getBytes().length + pic.getBytes().length + aFile.length() + end_data.length)); //设置内容长度
httpOAuthConsumer.setAdditionalParameters(para);
httpOAuthConsumer.sign(request);
OutputStream ot = request.getOutputStream();
ot.write(content.getBytes());
ot.write(status.getBytes());
ot.write(pic.getBytes());
ot.write(file);
ot.write(end_data);
ot.flush();
ot.close();
request.connect();
if( request.getResponseCode() == 200)
result = request.getResponseMessage();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
return result;
}