gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:49:51 27 4
gpt4 key购买 nike

我正在尝试从 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、$_FILES的vardump:

enter image description here

我希望 POST 有文件和变量

在我的 Eclipse Logcat 中,我在使用 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;

我想我已经正确地包含了所有必需的库:

enter image description here

编辑:

我能够解决这个问题,我的解决方案如下面的回答所述

最佳答案

我很幸运找到了一个Answer by Krystian在 Stackoverflow 中解决了我的问题。

添加边界后它开始工作。我必须将它添加到 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com