使用MultipartEntity将Android图片上传到服务器

6

我一直在尝试将图像和数据上传到Django服务器。我已经包含了apache-mime4j.0.6.jarhttpmime4.0.1.jar库(项目->构建路径->添加外部jar文件) 以下是上传图像的代码。

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

尝试创建包含库的新应用程序。然后我遇到了同样的错误。我已经尽力了。有人能告诉我为什么会发生这种情况以及如何解决它吗?非常感谢您的帮助!

4个回答

1

如果您正在使用新的android-sdk,请尝试以下方法。

  1. 创建名为libs的文件夹。
  2. 将您的jar文件粘贴到该文件夹中。
  3. 最后右键单击您的项目>构建路径>添加外部存档。

就是这样。希望能对您有所帮助。祝好运。


是的,我也这样做了。我已经提到过了。 - Geetanjali
你是否按照我提到的步骤进行操作?如果是,那么它就不应该给你“找不到类”错误。如果你只是通过你所使用的方式添加了库,那么这种方法是行不通的。因为这是新SDK中的更改。你之前做的在旧版本SDK中是正确的,但在新版中将不起作用。 :) - Akshay

0

我使用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();
    }
}

Jar库是否在libs文件夹中?这对于正确打包到APK中是必要的。您可以尝试将其删除并重新添加一次。直接从文件浏览器将其拖放到Eclipse中的文件夹中。然后您应该能够在“Android依赖项”下看到它,您可以打开它并查看其中是否有所需的类。 - SimonSays
你能给一个使用示例吗?这将非常有帮助。 - Droidman

0

我建议你使用spring-android。它是一个很好的适用于Android的rest API库。使用spring-android,你可以轻松地上传文件,就像这样。

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 HTTP 客户端时,对于 Android 2.3 及以上版本的 API 推荐使用 Java.net.URLConnection。这很容易上手。


什么是RestUtil?即使我导入了9个下载的包,它仍然无法解析。而YourResultDTO又是什么? - Droidman
你应该将YourResultDTO替换为你自己的DTO类。它将是保存来自服务器结果的结果对象,如果你正确设置了spring-android,spring-android会处理解析json / xml结果。 - kingori

0
我有同样的问题,我是这样解决的:在Java构建路径窗口中,点击“Order and Export”选项卡,突出显示您想要添加的库,然后单击上面的箭头(将库上传为第一个)。
点击确定,一切都好。
这里是带有照片的链接(Android Eclipse NoClassDefFoundError for external .jar files)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接