Why am I getting
ProtocolException: content-length promised 343 bytes, but received 0
in Android 4.x when using URLConnection
? while works fine in Android
2.3.3...
Stacktrace:
W/System.err(7560): java.net.ProtocolException: content-length promised 343 bytes, but received 0
W/System.err(7560): at com.android.okhttp.internal.http.RetryableOutputStream.close(RetryableOutputStream.java:52)
W/System.err(7560): at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:636)
W/System.err(7560): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
W/System.err(7560): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
W/System.err(7560): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
W/System.err(7560): at application._Utils.postFeedback(_Utils.java:95)
W/System.err(7560): at application.OptionsPreference.onClick(OptionsPreference.java:559)
W/System.err(7560): at android.view.View.performClick(View.java:4446)
W/System.err(7560): at android.view.View$PerformClick.run(View.java:18437)
W/System.err(7560): at android.os.Handler.handleCallback(Handler.java:733)
W/System.err(7560): at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err(7560): at android.os.Looper.loop(Looper.java:136)
W/System.err(7560): at android.app.ActivityThread.main(ActivityThread.java:5372)
W/System.err(7560): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(7560): at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err(7560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
W/System.err(7560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
W/System.err(7560): at dalvik.system.NativeStart.main(Native Method)
Code:
final URLConnection _uc = new URL(url).openConnection();
final String parameters = String.format("fullname=%s&email=%s&website=&comment=%s&settings_WITH_JS=1&commentJsError=&hide_mail=0", name, email, message);
_uc.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
_uc.setRequestProperty("Cache-Control:", "no-cache");
_uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*");
_uc.setRequestProperty("Origin", "http://x.com");
_uc.setRequestProperty("Upgrade-Insecure-Requests", "1");
_uc.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) "); // Fixes FileNotFoundException in some cases.
_uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);
_uc.setRequestProperty("Referer", referer);
_uc.setRequestProperty("Accept-Encoding", "gzip, deflate, lzma");
_uc.setRequestProperty("Accept-Language", "en-US,en;");
_uc.setRequestProperty("Accept-Charset", encoding); // Parameters encoding.
_uc.setConnectTimeout(timeout);
_uc.setReadTimeout(timeout);
_uc.setUseCaches(false);
_uc.setDoInput(true); // false = Ignores response body, and disallows use of getResponseCode().
_uc.setDoOutput(true); // true = POST/Put, and allows use of getOutputStream().
_uc.getOutputStream().write(parameters.getBytes(encoding)); // Put/POST.
_uc.getInputStream().close(); // Throws ProtocolException in Android +4!?
I've searched a lot but couldn't find anything... what's the problem?