android - 从Android将文件发布到服务器-MultipartEntityBuilder-HTTP

原文 标签 android http file-upload multipart

我正在尝试从android应用程序上传一些文件到我的服务器(通过http post),我在这里检查了很多问题,但无法让它工作。我希望有人能帮我:
我还在url中包含了一些变量,以验证至少这些变量到达了服务器(get)。

    HttpClient httpClient = new DefaultHttpClient();
    String url="http://XXXXX.com/files/upload.php?flies=yes&eats=no&friend=yes";
    HttpPost httppost = new HttpPost(url);
    httppost.addHeader("Content-Type", "multipart/form-data");
    String content,response="";
    try {

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addTextBody("randomvar", "42");

        //add image file
        //String filename="eye.png";
        //InputStream is=mContext.getAssets().open(filename);
        //builder.addBinaryBody("image", is, ContentType.create("image/png"), filename);

        //add text XML file
        final File file = new File(mContext.getFilesDir() +"/"+"serverXML.xml");
        FileBody fb = new FileBody(file);
        builder.addPart("file", fb);
        httppost.setEntity(builder.build());


        response = EntityUtils.toString(httpClient.execute(httppost).getEntity(), "UTF-8");


        Log.i(TAG, "RESPONSE FROM SERVER: "+response);
    } catch (IOException e) {
        Log.i(TAG, "Exception: "+response);
        e.printStackTrace();
    }

这会运行并从服务器获取响应。以下是$get、$post、$u文件的vardump:
我希望post有文件和变量
在我的eclipse日志中,使用multipartentitybuilder之后,我看到了以下内容:
10-06 02:36:57.231: D/dalvikvm(15888): DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueParser;.INSTANCE
10-06 02:36:57.241: W/dalvikvm(15888): VFY: unable to resolve static field 5966 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueParser;
10-06 02:36:57.241: D/dalvikvm(15888): VFY: replacing opcode 0x62 at 0x001b
10-06 02:36:57.241: D/dalvikvm(15888): DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueFormatter;.INSTANCE
10-06 02:36:57.241: W/dalvikvm(15888): VFY: unable to resolve static field 5960 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueFormatter;

我想我已经正确地包含了所有必需的库:
编辑:
我能够解决这个问题,我的解决方案如下

最佳答案

我很幸运在stackoverflow中找到一个Answer by Krystian解决了我的问题。
在添加边界之后,它开始工作。我不得不把它添加到http帖子中

String boundary = "-------------" + System.currentTimeMillis();
httppost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);

以及实体
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setBoundary(boundary);

现在我得到了服务器中的所有内容:
10-06 13:16:06.977: I/UploadFileAsync(31506): RESPONSE FROM SERVER: Array
10-06 13:16:06.977: I/UploadFileAsync(31506): (
10-06 13:16:06.977: I/UploadFileAsync(31506):     [flies] => yes
10-06 13:16:06.977: I/UploadFileAsync(31506):     [eats] => no
10-06 13:16:06.977: I/UploadFileAsync(31506):     [friend] => yes
10-06 13:16:06.977: I/UploadFileAsync(31506): )
10-06 13:16:06.977: I/UploadFileAsync(31506): Array
10-06 13:16:06.977: I/UploadFileAsync(31506): (
10-06 13:16:06.977: I/UploadFileAsync(31506):     [randomvar] => 42
10-06 13:16:06.977: I/UploadFileAsync(31506):     [mystr] => blaka
10-06 13:16:06.977: I/UploadFileAsync(31506): )
10-06 13:16:06.977: I/UploadFileAsync(31506): Array
10-06 13:16:06.977: I/UploadFileAsync(31506): (
10-06 13:16:06.977: I/UploadFileAsync(31506):     [file] => Array
10-06 13:16:06.977: I/UploadFileAsync(31506):         (
10-06 13:16:06.977: I/UploadFileAsync(31506):             [name] => serverXML.xml
10-06 13:16:06.977: I/UploadFileAsync(31506):             [type] => application/octet-stream
10-06 13:16:06.977: I/UploadFileAsync(31506):             [tmp_name] => /tmp/phpdmEsn2
10-06 13:16:06.977: I/UploadFileAsync(31506):             [error] => 0
10-06 13:16:06.977: I/UploadFileAsync(31506):             [size] => 3548
10-06 13:16:06.977: I/UploadFileAsync(31506):         )
10-06 13:16:06.977: I/UploadFileAsync(31506): )

关于android - 从Android将文件发布到服务器-MultipartEntityBuilder-HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26210679/

相关文章:

python - python BaseHttpServer是否支持HTML5?

javascript - 使用Jquery点击隐藏文件上传按钮?

javascript - 文件上传中的javascript正则表达式和文件类型

php - 自定义CSS/HTML浏览图像表单

java - 正在获取我/艺术: Explicit concurrent mark sweep GC freed

android - 如何像Wifi Analyzer App一样在android中绘制图形?

java - 如何在 Android 中更改警报对话框的文本行

http - 在 go net/http 中获取 http 正文

android - 恢复应用内购买的安全问题

jquery - 让 Node.js 中的 http.request 在浏览器中工作