Android advanced 2 Sina Weibo uses oauth to send pictures and text

Source: Internet
Author: User
Tags oauth

I thought Sina's publishing pictures are as simple as publishing text, but they are quite tragic ....... It is far from what we think.

Share it nowCode.

 

/** <Br/> * posting Weibo posts with images <br/> * @ Param token <br/> * @ Param tokensecret <br/> *@ param status <br/> * published content <br/> * @ Param urlpath <br/> * the address of the image is not used in this function, I created it directly in the function. <Br/> * @ return <br/> */<br/> Public static void uploadstatus (string token, string tokensecret, string imageurl, string status) <br/> {<br/> file files = environment. getexternalstoragedirectory (); <br/> string sdpath = files. getabsolutepath (); <br/> // ensure that the image file is in the root directory of the SD card. <br/> string urlpath = sdpath + "/" + "c.jpg "; <br/> file imagefile = new file (urlpath); <br/> If (! Imagefile. exists () <br/>{< br/> system. out. println ("this image is not available"); <br/>}< br/> httpoauthconsumer = new defaultoauthconsumer ("Your appkey", "Your appsecret "); // remember to replace it with yours <br/> httpoauthconsumer. settokenwithsecret (token, tokensecret); // The task before token tokensecret <br/> try <br/>{< br/> URL url = new URL (constants. xinlangupload); // URL: http://api.t.sina.com.cn/statuses/upload.json <br/> httpurlconnection request = (httpurlconnection) URL. openconnection (); <br/> request. setdooutput (true); <br/> request. setrequestmethod ("Post"); <br/> httpparameters = new httpparameters (); <br/> httpparameters. put ("status", urlencoder. encode (status, "UTF-8 "). replaceall ("\ +", "% 20"); <br/> string boundary = "--------------------------- 37531613912423 "; <br/> string content = "--" + boundary + "\ r \ ncontent-Disposition: Form-data; name = \ "status \" \ r \ n "; <br/> string PIC = "\ r \ n --" <br/> + boundary <br/> + "\ r \ ncontent-Disposition: Form-data; name = \ "pic \"; filename = \ "image.jpg \" \ r \ ncontent-type: image/JPEG \ r \ n "; <br/> byte [] end_data = ("\ r \ n --" + boundary + "-- \ r \ n "). getbytes (); <br/> fileinputstream stream = new fileinputstream (imagefile); <br/> byte [] file = new byte [(INT) imagefile. length ()]; <br/> stream. read (File); <br/> request. setrequestproperty ("Content-Type", "multipart/form-data; boundary =" + boundary); // set the form type and separator <br/> request. setrequestproperty (<br/> "Content-Length", <br/> string. valueof (content. getbytes (). length + status. getbytes (). length + pic. getbytes (). length <br/> + imagefile. length () + end_data.length); // set the content length <br/> httpoauthconsumer. setadditionalparameters (httpparameters); <br/> httpoauthconsumer. sign (request); <br/> outputstream = request. getoutputstream (); <br/> outputstream. write (content. getbytes (); <br/> outputstream. write (status. getbytes (); <br/> outputstream. write (PIC. getbytes (); <br/> outputstream. write (File); <br/> outputstream. write (end_data); <br/> outputstream. flush (); <br/> outputstream. close (); <br/> request. connect (); <br/> If (200 = request. getresponsecode () <br/>{< br/> system. out. println ("published successfully"); <br/>}< br/> else <br/>{< br/> system. out. println ("Exception Code:" + request. getresponsecode (); <br/>}< br/> catch (exception E1) <br/>{< br/> e1.printstacktrace (); <br/>}< br/>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.