This is my code.
I got Http 400 error, can someone help me?
HttpClient httpClient;
HttpPost httpPost;
HttpResponse response;
HttpContext localContext;
FileEntity tmp = null;
String ret = null;
httpClient = new DefaultHttpClient( );
httpClient.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109) ;
httpPost = new HttpPost(url);
tmp = new FileEntity( 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 the photo" );
localContext = new BasicHttpContext( );
response = httpClient.execute( httpPost,localContext );
bobince, thanks this is my new id, I will try put OAuth to my connection header.
And this is my old code, I will update it soon.
private void uploadPicture( ) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams( ).setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1 );
HttpPost httppost = new HttpPost( "https://graph.facebook.com/me/photos" );
File file = new File( sdpicturePath );
// DEBUG
Log.d( "TSET", "FILE::" + file.exists( ) ); // IT IS NOT NULL
Log.d( "TEST", "AT:" + fbAccessToken ); // I GOT SOME ACCESS TOKEN
MultipartEntity mpEntity = new MultipartEntity( );
ContentBody cbFile = new FileBody( file, "image/png" );
ContentBody cbMessage = new StringBody( "TEST TSET" );
ContentBody cbAccessToken = new StringBody( fbAccessToken );
mpEntity.addPart( "access_token", cbAccessToken );
mpEntity.addPart( "source", cbFile );
mpEntity.addPart( "message", cbMessage );
httppost.setEntity( mpEntity );
// DEBUG
System.out.println( "executing request " + 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( );
} // end of uploadPicture( )
To do this, post the photo to the Memories resource with the Content-Type set to multipart/form-data . Provide the description of each resource in the first part. Include the title and the qualifiers and any other metadata. Provide the images in each subsequent part with the Content-Type set to image/jpeg .
Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.
setEntity
sets the source of the whole request body, so this would only work if the data
file was an already-encoded multipart/form-data
block.
To create a multipart/form-data
-encoded form submission for use as a POST request body, you'll need a MIME multipart encoder, typically org.apache.http.entity.mime.MultipartEntity
. Unfortunately, this is not bundled by Android so if you want it you'll have to pull in a newer HttpClient from Apache.
See this question for example code and this thread for background.
There is my working solution for sending image with post, using apache http libraries:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(StaticData.AMBAJE_SERVER_URL + StaticData.AMBAJE_ADD_AMBAJ_TO_GROUP);
String boundary = "-------------" + System.currentTimeMillis();
httpPost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
ByteArrayBody bab = new ByteArrayBody(imageBytes, "pic.png");
StringBody sbOwner = new StringBody(StaticData.loggedUserId, ContentType.TEXT_PLAIN);
StringBody sbGroup = new StringBody("group", ContentType.TEXT_PLAIN);
HttpEntity entity = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.setBoundary(boundary)
.addPart("group", sbGroup)
.addPart("owner", sbOwner)
.addPart("image", bab)
.build();
httpPost.setEntity(entity);
try {
HttpResponse response = httpclient.execute(httpPost);
...then reading response
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With