【问题标题】:Android - image upload sending no contentAndroid - 图片上传不发送内容
【发布时间】:2011-02-18 15:00:23
【问题描述】:

过去一两天我一直在研究这个问题,但似乎找不到解决问题的方法。我正在尝试使用 httppost 将图像发布到服务器。 我尝试了两种方法,都完成了帖子,但没有内容,即内容长度为 0。 第一种如下:

String url = "MYURL";
        HttpClient httpClient = new DefaultHttpClient();

     try {
             httpClient.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second
             HttpPost post = new HttpPost(url);
             File SDCardRoot = Environment.getExternalStorageDirectory();
             File file = new File(SDCardRoot,"/DCIM/100MSDCF/DSC00004.jpg");
             FileEntity entity;
             entity = new FileEntity(file,"binary/octet-stream");
             entity.setChunked(true);
             post.setEntity(entity);
             post.addHeader("Header", "UniqueName");
             HttpResponse response = httpClient.execute(post);
             if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                     Log.e("Here","--------Error--------Response Status line code:"+response.getStatusLine());
             }else {
                     // Here every thing is fine.
             }

             HttpEntity resEntity = response.getEntity();
             if (resEntity == null) {
                 Log.e("Here","---------Error No Response!!-----");
             }
     } catch (Exception ex) {
             Log.e("Here","---------Error-----"+ex.getMessage());
             ex.printStackTrace();
     } finally {
               httpClient.getConnectionManager().shutdown();
     }

第二个是:

String url = "MYURL";
        //File SDCardRoot = Environment.getExternalStorageDirectory();
        File file = new File(Environment.getExternalStorageDirectory(),"/DCIM/100MSDCF/DSC00004.jpg");
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
            reqEntity.setContentType("binary/octet-stream");
            reqEntity.setChunked(true);
            // Send in multiple parts if needed
            httppost.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(httppost);
            Log.d("finishing", "The try catch function");
        } catch (Exception e) {
            // show error
        }*/

如您所见,我已经硬编码了一个特定图像的路径,当我启动并运行它时,它是动态的。 谁能看到我做错了什么?我遗漏了什么吗?我知道我使用 setChunked 和 setContenttype - 是否有 setContent 选项? 任何帮助将不胜感激。

谢谢, JR83.

【问题讨论】:

    标签: android http-post


    【解决方案1】:

    您可以通过发送多部分消息来上传您的图片;您可能会发现 this 讨论很有用。

    【讨论】:

      猜你喜欢
      • 2011-01-04
      • 2020-09-03
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2012-08-12
      相关资源
      最近更新 更多