Skip to main content.

公告

今天把博客开通了,今后就给大家分享一些关于Android的信息.

搜索

友情链接

Android实现新浪微博Oauth发送图片和文字

基于网上很多人利用新浪api开发新浪微博客户端的时候遇到无法发图片的问题,很多人卡在了这一步。

现将代码呈上,希望能帮到一些朋友。

 /** 
       * 发表带图片的微博 
       * @param token 
       * @param tokenSecret 
       * @param aFile     要上传的图片文件
       * @param status    要发表的微博文字
       * @param urlPath   对应的api地址
       * @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;  
     }   
好了,就这样

标签: Android, 新浪微博, Oauth
Posted by android_boy @ 2011-7-6 12:45:23 阅读(1829) 评论(1)
上一篇:Android如何发送彩信
下一篇:Android中渐变动画演示(Tween Animation)

Feedback

  1. 回复 2011-8-1 20:53:48 by iStar

    Thanks so much !
你还可以输入600/600个字符 发表评论
称呼: (必填) 登录 | 开通博客
邮箱: (选填) 你的邮箱地址不会被公开
网站: (选填)
验证码: (必填)