facebook - How use multipart/form-data upload picture/image on Android -


This is my code.

I got the HTTP 400 error, can someone help me?

HTTP Client http Client; Http post http post; HTTP response response; HttpContext Local Contestect; FileTypeTMP = Faucet; String rate = null; HttpClient = new DefaultHttpClient (); HttpClient.getParams () SetParameter (Client PNames.COOKIE_POLICY, CookiePolicy.RFC_2109); HttpPost = New HTTP Post (URL); TMP = new filetyt (data, "UTF-8"); HttpPost.setEntity (tmp); HttpPost.setHeader ("content-type", "multipart / form-data"); HttpPost.setHeader ("access_token", facebook.getAccessToken ()); HttpPost.setHeader ("source", data. GetAbsolutePath ()); HttpPost.setHeader ("Message", "Caption for Photo"); LocalContext = New BasicHttpContext (); Response = httpClient.execute (httpPost, localContext);

bobince, thanks, this is my new id, I will try to put Oeth in my connection headers.

And this is my old code, I will update it soon.

  Upload private zodiac (photo) Foursquare ParseException, IOException {HttpClient httpclient = New DefaultHttpClient (); Httpclient.getParams () .setParameter (Core Protocol PNames.PROTOCOL_VERSION, http version.ttp_1_1); Http post httppost = new HTTP post ("https://graph.facebook.com/me/photos"); File file = new file (sdpTeacherpath); // DEBUG Log.d ("TSET", "FILE ::" + file.exists ()); // IT tap is not log D ("test", "AT:" + FBAetToken); // I have some access tokens multipart ntpep mpEntity = new multiparty (); ContentBody cbFile = new file body (file, "image / PNG"); Material Body cbMessage = New Stringbody ("Test TSE"); Content Body cbAccessToken = New StringBody (fbAccessToken); MpEntity.addPart ("access_token", cbAccessToken); MpEntity.addPart ("source", cbFile); MpEntity.addPart ("Message", cbMessage); Httppost.setEntity (mpEntity); // DEBUG System.out.println ("Request execution" + httppost.getRequestLine ()); HttpResponse response = httpclient.execute (httppost); HttpEntity resEntity = response.getEntity (); // DEBUG System.out.println (response.getStatusLine ()); If (resEntity! = Null) {System.out.println (EntityUtils.toString (resEntity)); } // end if if (resEntity! = Null) {resEntity.consumeContent (); } // end if httpclient.getConnectionManager () .shutdown (); } // upload end / picture ()  

setEntity Set source of the entire request set, so it will only work if data file is already encoded multipart / form-data block.

To create a multipart / form-data -encoded form submission for use as a POST request body, you will need an MIME Multipart Encoder, usually < Code> org.apache.http.entity.mime MultipartEntity . Unfortunately, it is not bundled by Android, if you want it, then you have to draw a new one from Apache.

See for example code and background.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -