Android上如何使用multipart

Android上如何使用multipart

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

问题描述

这是我的代码.

我收到 Http 400 错误,有人可以帮助我吗?

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,谢谢这是我的新 ID,我会尝试将 OAuth 放到我的连接头中.


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 设置整个请求正文的来源,因此这仅在 data 文件时有效是一个已经编码的 multipart/form-data 块.

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.

要创建一个 multipart/form-data 编码的表单提交以用作 POST 请求正文,您需要一个 MIME 多部分编码器,通常是 org.apache.http.entity.mime.MultipartEntity.不幸的是,这不是由 Android 捆绑的,所以如果你想要它,你必须引入一个更新的 HttpClient 来自 Apache.

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.

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

08-05 19:53