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/>}