我正在尝试使用帖子将数据发送到Web服务器,这是我到目前为止的内容:

private void entity(String id, String file)
                throws JSONException, UnsupportedEncodingException, FileNotFoundException {
             // Add your data
            File myFile = new File(
                    Environment.getExternalStorageDirectory(), file);
            InputStreamEntity reqEntity = new InputStreamEntity(
                    new FileInputStream(myFile), myFile.length());

            reqEntity.setContentType("text/csv");
            reqEntity.setChunked(true); // Send in multiple parts if needed

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("project", id));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httppost.setEntity(reqEntity);

            //httppost.setHeader("Content-Length", String.valueOf(myFile.length()));

        }


当我发送发帖请求时,它返回的内容长度为必填项,但这不是在此处设置的吗?

InputStreamEntity reqEntity = new InputStreamEntity(
                    new FileInputStream(myFile), myFile.length());


我不知道我在做什么是否正确,请帮助,谢谢



编辑-

当我尝试使用自己设置内容长度时

httppost.setHeader("Content-Length", String.valueOf(myFile.length()));


它返回时已经设置了标头。

因此为何将其注释掉

最佳答案

您可能要使用MultipartEntity并通过addPartStringBody(或)InputStreamBody使用FileBody方法添加零件

10-05 21:00
查看更多