Android使用MultipartEntity将图像发布到服务器

Gee*_*ali 6 django android dalvik multipartentity

我一直在尝试将图像和数据上传到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");
  } 
Run Code Online (Sandbox Code Playgroud)

错误信息:

Could not find class 'org.apache.http.entity.mime.MultipartEntity'
Run Code Online (Sandbox Code Playgroud)

我尝试手动创建libs文件夹并手动将jar文件包含到/ libs文件夹中.当我这样做它无法编译.

错误:

Conversion to Dalvik format failed with error 1  Unknown Android Packaging Problem
Run Code Online (Sandbox Code Playgroud)

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

Sim*_*ays 0

我使用 .NET 将图像从 Android 上传到 Django 服务器httpmime-4.2.1.jar。这是我包含的唯一库,并且运行良好。顺便说一句:库应该位于 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();
    }
}
Run Code Online (Sandbox Code Playgroud)