Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpURLConnection POST gives HTTP error 411

I'm having problems with POST request and when running on emulator.

Problem with device: When sending POST request I am getting this 411 error (Content-Length required). Adding that request property doesn't help.

Problem with emulator: When typing REST url to web client, it works. But when running it with the application client, I get UnkownHostExpection.

This far I am pretty clueless what to be done to fix my issues, so I was hoping you could point out some tips to solve these.

Thanks.

Here is relevant code:

    URL url = new URL("http://etc..");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.addRequestProperty("Accept", "application/json");
    conn.addRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Accept-Charset", CHARSET_UTF8);

    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setChunkedStreamingMode(0);
    conn.getOutputStream().write(myPostParams.getBytes(CHARSET_UTF8));

    conn.connect();
like image 319
Niko Avatar asked Aug 15 '26 20:08

Niko


1 Answers

Have you tried setting to use setFixedLengthStreamingMode() instead of setChunkedStreamingMode(0)?

like image 91
pohtzeyun Avatar answered Aug 18 '26 10:08

pohtzeyun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!