我一直试图将图像和数据上传到Django服务器。我已经包含了apache-mime4j.0.6.jar和httpmime4.0.1.jar库( Project->build >Add文件),下面是上传图像的代码。
HttpResponse response = null;
try {
HttpPost httppost = new HttpPost("http://10.0.2.2:8000/mobile");
// HttpPost httppost = new HttpPost("some url");
MultipartEntity multipartEntity = new MultipartEntity(); //MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("name", new StringBody("nameText"));
multipartEntity.addPart("place", new StringBody("placeText"));
multipartEntity.addPart("tag", new StringBody("tagText"));
//multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));
multipartEntity.addPart("Image", new FileBody(destination));
httppost.setEntity(multipartEntity);
httpclient.execute(httppost, new PhotoUploadResponseHandler());
} catch (Exception e) {
Log.e( "Error","error");
} 错误消息:
Could not find class 'org.apache.http.entity.mime.MultipartEntity'我尝试手动创建libs文件夹,并手动将jar文件包含到/libs文件夹中。当我这样做时,它无法编译。
错误:
Conversion to Dalvik format failed with error 1 Unknown Android Packaging Problem尝试创建新的应用程序,包括库。我也遇到了同样的错误。我已经尝试了一切可能的方法。有谁能告诉我为什么会发生这种事,以及如何解决。任何帮助都将不胜感激!!
发布于 2012-11-02 06:16:40
如果您正在使用新的android-sdk,请尝试以下操作。
libs的文件夹那是it.This可能help.Good Luck。
发布于 2012-11-02 06:16:27
我使用httpmime-4.2.1.jar将一个图像从Android上传到Django服务器。这是我所包含的唯一一个库,它运行良好。顺便说一句:库应该在Android项目的libs文件夹中。
这是我在上传时使用的代码。
private JSONObject uploadImage(String url, File img) throws Exception{
url = addAuthToken(url);
HttpPost post = new HttpPost(url);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(img, "images/jpeg");
reqEntity.addPart("image", fileBody);
post.setEntity(reqEntity);
JSONObject ret = baseRequest(post);
checkReturnStatus(ret, 201);
return ret;
}
private JSONObject baseRequest(HttpUriRequest request) throws Exception{
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
BufferedReader in = null;
try{
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer();
String line = null;
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
return new JSONObject(sb.toString());
}finally {
if(in != null) in.close();
}
}发布于 2012-11-02 06:59:10
我建议您使用spring-android,这是一个很好的rest库,对于android来说,使用spring,您可以像这样轻松地上传文件。
HttpHeaders requestHeaders = ....
MultiValueMap<String, Object> message = new LinkedMultiValueMap<String, Object>();
message.add("qqfile", new FileSystemResource(filePath)); //attach file
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(
message, requestHeaders);
RestTemplate restTemplate = RestUtil.getRestTemplate( context);
ResponseEntity<YourResultDTO> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,YourResultDTO.class);它非常容易使用和使用Apache客户端的前姜饼和java.net.urlconnection的姜饼和更高的api水平,如谷歌所建议的。
https://stackoverflow.com/questions/13190009
复制相似问题