首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

新浪微博采取Oauth发送图片和文字

2012-07-20 
新浪微博采用Oauth发送图片和文字基于网上很多人利用新浪api开发新浪微博客户端的时候遇到无法发图片的问

新浪微博采用Oauth发送图片和文字

基于网上很多人利用新浪api开发新浪微博客户端的时候遇到无法发图片的问题,很多人卡在了这一布。现将代码呈上,希望能帮到一些朋友。

?

?

/** * 发表带图片的微博 * @param token * @param tokenSecret * @param aFile * @param status * @param urlPath * @return */public String uploadStatus(String token, String tokenSecret, File aFile, String status, String urlPath) {httpOAuthConsumer = new DefaultOAuthConsumer(consumerKey,consumerSecret);        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+"\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";        byte[] end_data = ("\r\n--" + boundary + "--\r\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 (200 == request.getResponseCode()) {        result = "SUCCESS";        }} 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;} 
1 楼 Crazymn007 2011-11-18   你好,我遇到的问题是带图片的微博可以同非中文内容一起发送。跟中文的一起发送就不行了。这是什么问题呢?求解答 2 楼 mzba520 2011-11-18   Crazymn007 写道你好,我遇到的问题是带图片的微博可以同非中文内容一起发送。跟中文的一起发送就不行了。这是什么问题呢?求解答

你有没有对中文进行编码啊? 3 楼 zp8126 2011-11-29   我的是获得request.getResponseCode()老是403,为什么啊,你这里的urlPath是什么啊,String token, String tokenSecret和httpOAuthConsumer = new DefaultOAuthConsumer(consumerKey,consumerSecret);  这些都分别是什么呢 ,谢谢啊 4 楼 mzba520 2011-11-30   zp8126 写道我的是获得request.getResponseCode()老是403,为什么啊,你这里的urlPath是什么啊,String token, String tokenSecret和httpOAuthConsumer = new DefaultOAuthConsumer(consumerKey,consumerSecret);  这些都分别是什么呢 ,谢谢啊

token,tokenSecret是你从新浪那里获取的。
consumerKey,consumerSecret是你去申请app key的时候得到的

热点排行