0

I am trying to send data to a web server using a post, this is what I have so far:

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()));

        }

When I send the post request it comes back with content-length required, but isn't that set here?

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

I don't know if what I am doing is right or not, please help, thanks


Edit -

When I try to set the content-length myself using

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

it comes back with header already set.

Hence why it is commented out

FabianCook
  • 19,591
  • 16
  • 64
  • 112

3 Answers3

1

In my case using

reqEntity.setChunked(false);

solved the problem.

Jasper de Vries
  • 16,868
  • 6
  • 60
  • 93
0

That sets the length of the content for the InputStreamEntity which is used locally when writing the data to the OutputStream. It doesn't set that value as a parameter for your request. You'll need to set that header yourself.

Jason Robinson
  • 30,410
  • 19
  • 76
  • 130
0

You may want to use MultipartEntity and add parts using addPart methods with StringBody and InputStreamBody (or) FileBody

srkavin
  • 1,142
  • 7
  • 17