我想上传一些文件到我的服务器(通过HTTP POST)从一个Android应用程序,我在这里检查过很多问题,但不能让它开始工作。我希望有人能帮助我了:
I am trying to upload some files to my server (via HTTP POST) from an Android app and I have checked many Questions here but can't get it to work. I hope someone can help me out:
我还包含在URL中的一些变量来验证至少那些到达服务器(GET)。
I also included some variables in the URL to verify that at least those are reaching the server(GET).
HttpClient httpClient = new DefaultHttpClient();
String url="https://m.xsw88.com/allimgs/daicuo/20230907/6493.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的vardump,$ _FILES:
This does run and get a response from the server. The following is the vardump of $_GET, $_POST, $_FILES:
我想POST有文件和可变
I would want POST to have the file and variable
在我的Eclipse的logcat我看到使用MultiPartEntityBuilder后的情况如下:
In my Eclipse Logcat I see the following after using 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;
我想我已经正确包含所有需要的库:
I think I have correctly included all the required libraries:
编辑:
我能解决这个问题,我的解决方案是我的回答说明
I was able to solve this, my solution is the described in my answer below
我很幸运地找到一个Answer由克里斯蒂安在#1就解决了我的问题。
I was fortunate enough to find one Answer by Krystian in Stackoverflow that solved my issue.
添加边界后,它开始工作。我不得不将它添加到相应的HTTP POST
After adding the boundary it started working. I had to add it to the HTTP Post
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);
现在我得到的一切,在服务器:
Now I get everything in the server:
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): )