首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MultipartEntity将图像发布到服务器

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

Stack Overflow用户
提问于 2012-11-02 05:49:10
回答 4查看 11.7K关注 0票数 6

我一直试图将图像和数据上传到Django服务器。我已经包含了apache-mime4j.0.6.jarhttpmime4.0.1.jar库( Project->build >Add文件),下面是上传图像的代码。

代码语言:javascript
复制
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");
  } 

错误消息:

代码语言:javascript
复制
Could not find class 'org.apache.http.entity.mime.MultipartEntity'

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

错误:

代码语言:javascript
复制
Conversion to Dalvik format failed with error 1  Unknown Android Packaging Problem

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

EN

回答 4

Stack Overflow用户

发布于 2012-11-02 06:16:40

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

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

那是it.This可能help.Good Luck。

票数 1
EN

Stack Overflow用户

发布于 2012-11-02 06:16:27

我使用httpmime-4.2.1.jar将一个图像从Android上传到Django服务器。这是我所包含的唯一一个库,它运行良好。顺便说一句:库应该在Android项目的libs文件夹中。

这是我在上传时使用的代码。

代码语言:javascript
复制
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();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2012-11-02 06:59:10

我建议您使用spring-android,这是一个很好的rest库,对于android来说,使用spring,您可以像这样轻松地上传文件。

代码语言:javascript
复制
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水平,如谷歌所建议的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13190009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档