【问题标题】:Android post image to the Server using MultipartEntityAndroid 使用 MultipartEntity 将图像发布到服务器
【发布时间】:2012-11-02 05:49:10
【问题描述】:

我一直在尝试将图像和数据上传到 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

尝试创建新的应用程序,包括库。我遇到了同样的错误。我已经尝试了所有可能的方法。谁能告诉我为什么会发生这种情况以及如何解决它。任何帮助将不胜感激!!

【问题讨论】:

    标签: android django dalvik multipartentity


    【解决方案1】:

    如果您使用的是新的android-sdk 试试这个。

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

    就是这样。这可能会有所帮助。祝你好运。

    【讨论】:

    • 是的,我也这样做了。我已经提到了
    • 您是否按照我提到的步骤进行操作,因为它不应该给您找不到类的错误。如果您只是使用您所做的方式添加库将不起作用。因为这是新的更改sdk. 你所做的对以前的 sdk 是正确的,但对于新的它将不起作用。 :)
    【解决方案2】:

    我使用 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 Dependencies”下看到它,您可以在其中打开它并查看所需的类是否在其中。
    • 你能给个用法例子吗..?会很有帮助
    【解决方案3】:

    我建议你使用spring-android 这是一个很好的android 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);
    

    正如谷歌建议的那样,在姜饼和更高的 api 级别上使用 Apache http 客户端和姜饼上的 java.net.urlconnection 非常容易。

    【讨论】:

    • 什么是 RestUtil?导入了9个下载的包也无法解决..YourResultDTO是什么?
    • 您应该将 YourResultDTO 替换为您自己的 DTO 类。它将是保存来自服务器的结果的结果对象,如果您正确设置 spring-android,spring-android 将处理解析 json / xml 结果。
    【解决方案4】:

    我遇到了同样的问题,我通过这种方式解决了:在 java 构建路径窗口中,单击 Order and Export 选项卡 突出显示要添加的库,然后单击向上(首先上传 lib)

    点击确定,一切正常。

    这是一个带有照片的链接 (Android Eclipse NoClassDefFoundError for external .jar files)

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 2012-09-07
      • 2015-07-16
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      相关资源
      最近更新 更多