如何使用的multipart / form-data的上传图片/ Android上的图像 [英] How use multipart/form-data upload picture/image on Android

查看:277
本文介绍了如何使用的multipart / form-data的上传图片/ Android上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code。

我得到HTTP 400错误,有人可以帮我吗?

  HttpClient的HttpClient的;
HttpPost httpPost;
HTT presponse响应;
HttpContext的localContext;
FileEntity TMP = NULL;
字符串RET = NULL;

HttpClient的=新DefaultHttpClient();
httpClient.getParams()的setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109)。

httpPost =新HttpPost(URL);
TMP =新FileEntity(数据,UTF-8);

httpPost.setEntity(TMP);
httpPost.setHeader(内容类型,多部分/表单数据);
httpPost.setHeader(access_token,facebook.getAccessToken());
httpPost.setHeader(源,data.getAbsolutePath());
httpPost.setHeader(信息,标题的照片);

localContext =新BasicHttpContext();
响应= httpClient.execute(httpPost,localContext);
 


bobince,由于这是我的新的ID,我会尽量把OAuth的,以我的连接头。

这是我的老code,我会尽快更新它。

 私人无效uploadPicture()抛出ParseException的,IOException异常{
    HttpClient的HttpClient的=新DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);

    HttpPost httppost =新HttpPost(https://graph.facebook.com/me/photos);
    档案文件=新的文件(sdpicturePath);

    // DEBUG
    Log.d(TSET,FILE ::+ file.exists()); // IT IS NOT NULL
    Log.d(TEST,AT+ fbAccessToken); //我得到了一些访问令牌

    MultipartEntity mpEntity =新MultipartEntity();
    ContentBody cbFile =新FileBody(文件,图像/ PNG);
    ContentBody cbMessage =新StringBody(TEST TSET);
    ContentBody cbAccessToken =新StringBody(fbAccessToken);

    mpEntity.addPart(access_token,cbAccessToken);
    mpEntity.addPart(源,cbFile);
    mpEntity.addPart(信息,cbMessage);

    httppost.setEntity(mpEntity);

    // DEBUG
    的System.out.println(执行请求+ httppost.getRequestLine());
    HTT presponse响应= httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    // DEBUG
    的System.out.println(response.getStatusLine());
    如果(resEntity!= NULL){
      的System.out.println(EntityUtils.toString(resEntity));
    如果} //结束

    如果(resEntity!= NULL){
      resEntity.consumeContent();
    如果} //结束

    httpclient.getConnectionManager().shutdown();
uploadPicture的} //结束()
 

解决方案

setEntity 设置整个请求体的来源,所以这只会工作,如果数据文件是一个已经连接codeD 的multipart / form-data的块。

要创建的multipart / form-data的克斯codeD的形式提交用作POST请求的身体,你需要一个MIME多恩codeR,一般 org.apache.http.entity.mime.MultipartEntity 。不幸的是,这是不是由Android的捆绑,所以如果你想要你就必须在一个新的 HttpClient的来自Apache拉。

请参阅<一href="http://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pro/1068132#1068132">this问题例如:code和这个线程的背景。

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( )

解决方案

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.

这篇关于如何使用的multipart / form-data的上传图片/ Android上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆