提问了很多次了,但是没有人回答,没办法,就再发帖了。
这个问题折磨我好几天了,还请明白的人给说说啊。
我用的是oauth-signpost的开源项目做的,发文本很简单,已经实现了,但是不知道怎么发送图片。
用了网上的一个方法是用HttpURLConnection请求但是报错。
以下代码
public String uploadStatus(String token, String tokenSecret, byte[] file, String status, String urlPath) {
URL url;
String result = "";
byte[] end_data = null;
HttpURLConnection request = null;
HttpParameters parames = new HttpParameters();
commonsHttpOAuthConsumer = new CommonsHttpOAuthConsumer(APP_KEY, APP_SECRET);
commonsHttpOAuthConsumer.setTokenWithSecret(token,tokenSecret);

String boundary = "---------------------------aaaaaaaaaaaaaaa";
String content = "--"
+ boundary
+ "\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\n";
String pic = "\r\n--"
+ boundary
+ "\r\nContent-Disposition: form-data; name=\"pic\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
end_data = ("\r\n--" + boundary + "--\r\n").getBytes();

try {
url = new URL(urlPath);
request = (HttpURLConnection) url.openConnection();
request.setDoOutput(true);
request.setRequestMethod("POST");
// 设置表单类型和分隔符
request.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
// 设置内容长度
request.setRequestProperty("Content-Length", String.valueOf(content.getBytes().length 
+ status.getBytes().length + pic.getBytes().length + file.length + end_data.length));

parames.put("status", URLEncoder.encode(status, "utf-8").replaceAll("\\+", " "));
commonsHttpOAuthConsumer.setAdditionalParameters(parames);
commonsHttpOAuthConsumer.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();
result = String.valueOf(request.getResponseCode()); System.out.println("result " + result);
} catch (Exception e) {
System.out.println("uploadStatus is exception " + e.getMessage());
e.printStackTrace();
} finally {
request.disconnect();
}

return result;
}
说请求的类型应该是HttpRequest,就是这句commonsHttpOAuthConsumer.sign(request);
不知道为什么。
或者说不用oauth-signpost直接用新浪提供的接口怎么实现啊?
我用new Weibo().uploadStatus(content, file);也报错啊,到底该怎么办?求救啊!