Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android使用MultipartEntity将图像发布到服务器_Android_Django_Dalvik_Multipartentity - Fatal编程技术网

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

Android使用MultipartEntity将图像发布到服务器,android,django,dalvik,multipartentity,Android,Django,Dalvik,Multipartentity,我一直在尝试将图像和数据上传到Django服务器。我已经包括了apache-mime4j.0.6.jar和httpmime4.0.1.jar库(项目->构建路径->添加外部jar文件) 这是上传图像的代码 HttpResponse response = null; try { HttpPost httppost = new HttpPost("http://10.0.2.2:8000/mobile"); // HttpPost httppost = new HttpPost("

我一直在尝试将图像和数据上传到Django服务器。我已经包括了
apache-mime4j.0.6.jar
httpmime4.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

尝试创建新的应用程序,包括库。我也遇到了同样的错误。我已经尽了一切可能。有人能告诉我为什么会发生这种情况,以及如何修复它吗。任何帮助都将不胜感激

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

如果您正在使用新的
android sdk
,请尝试此操作

  • 创建名为
    libs的文件夹
  • 将jar文件粘贴到该文件夹中
  • 最后右键单击项目>构建路径>添加外部归档
  • 就这样。这可能会有帮助。祝你好运。

    我建议你使用 这是一个很好的android RESTAPI库,使用SpringAndroid,您可以像这样轻松上传文件

    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);
    
    HttpHeaders-requestHeaders=。。。。
    多值映射消息=新链接的多值映射();
    添加(“qqfile”,新文件系统资源(filePath))//附加文件
    HttpEntity requestEntity=新HttpEntity(
    消息、请求头);
    RestTemplate RestTemplate=RestUtil.getRestTemplate(上下文);
    ResponseEntity response=restemplate.exchange(url、HttpMethod.POST、requestEntity、yourResultTo.class);
    

    正如谷歌所建议的,在pre gingerbread上使用Apache http client和在gingerbread上使用java.net.urlconnection以及更高的api级别是非常容易的。

    我遇到了同样的问题,我这样解决了它:在java build path窗口中,单击Order and Export选项卡突出显示要添加的库,然后单击up(将库上载为第一个)

    单击ok,一切正常


    这里有一个照片链接()

    是的,我也这么做了。我提到过我是否遵循了我提到的步骤,因为它不应该给您类未找到错误。如果您只是使用您没有使用的方式添加库,将无法工作。因为这是新sdk中的更改。您对以前的sdk所做的操作是正确的,但对新sdk所做的操作将无法工作。:)jar库是否在libs文件夹中?这是将其正确装入APK所必需的。您可以删除并再次添加它。将它从文件资源管理器直接拖动到eclipse中的文件夹中。然后你应该在“Android依赖项”下看到它,在那里你可以打开它,看看所需的类是否在其中。。?非常有用什么是RestUtil?即使我导入了9个下载的软件包,也无法解决此问题。。您的ResultTo是什么?您应该将您的ResultTo替换为您自己的DTO类。它将是保存来自服务器的结果的result对象,如果您正确设置springandroid,springandroid将处理解析json/xml结果。