【发布时间】:2013-11-29 11:19:19
【问题描述】:
我正在尝试将拍摄的照片上传到服务器。这就是我所做的:
public JSONObject makePostFileRequest(String url, String photoFile) {
try {
// photoFile = /path/tofile/pic.jpg
DefaultHttpClient httpClient = GlobalData.httpClient;
HttpPost httpPost = new HttpPost(url);
File file = new File(photoFile);
FileBody fileBody = new FileBody(file); // here is line 221
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("PhotoMessage", fileBody);
httpPost.setEntity(multipartEntity.build());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
我收到此错误:
11-29 13:12:14.924:E/AndroidRuntime(15781):由以下原因引起: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType 11-29 13:12:14.924: E/AndroidRuntime(15781): 在 org.apache.http.entity.mime.content.FileBody.(FileBody.java:89) 11-29 13:12:14.924: E/AndroidRuntime(15781): 在 com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:221)
我做错了什么?
更新
InputStream inputStream;
inputStream = new FileInputStream(new File(photoFile));
byte[] data;
data = IOUtils.toByteArray(inputStream);
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
System.getProperty("http.agent"));
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "Pic.jpg");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("PhotoMessage", inputStreamBody);
httpPost.setEntity(multipartEntity.build());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
这是错误:
11-29 14:00:33.364: E/AndroidRuntime(19478): 由: java.lang.NoClassDefFoundError:org.apache.http.util.Args 11-29 14:00:33.364:E/AndroidRuntime(19478):在 org.apache.http.entity.mime.content.AbstractContentBody.(AbstractContentBody.java:48) 11-29 14:00:33.364: E/AndroidRuntime(19478): 在 org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:69) 11-29 14:00:33.364: E/AndroidRuntime(19478): 在 org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:62) 11-29 14:00:33.364: E/AndroidRuntime(19478): 在 com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:233)
这些库解决了我的问题:
【问题讨论】:
-
FileBody不属于标准 Android 库。你用的是什么罐子? -
你在项目中添加了httpmime-4.3.jar吗?
-
当然。在 libs 文件夹中
-
您使用的是什么 IDE?蚀?如果是这样,请确保已添加并导出它,as in this answer
-
嗨,@FilipLuch 你找到解决这个问题的办法了吗?
标签: android httprequest