Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Android
URLConnection -> ProtocolException in Android 4
Fooasdsad Baraasdasd
Greenhorn
Posts: 5
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi
Why 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 = Force POST, and allows use of getOutputStream(). _uc.getOutputStream().write(parameters.getBytes(encoding)); // Put/POST. _uc.getInputStream().close(); // Throws ProtocolException in Android +4!?
What's the problem?
I've searched
alot
, but couldnt find the solution
Thanks in advance.
Paul Clapham
Marshal
Posts: 28177
95
I like...
posted 7 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Is that all the information you have about the error? Just its name? No stack trace or description or anything?
Here's a guess, then: I assume the String.format() method doesn't URL-encode the parameter values. Perhaps
you should
be doing that.
Fooasdsad Baraasdasd
Greenhorn
Posts: 5
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Sorry, forgot to post it... edited now.
And yea, already tried "URLEncoder.encode(X, encoding)" on those parameters but gives same error.
Fooasdsad Baraasdasd
Greenhorn
Posts: 5
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Up... :/
Fooasdsad Baraasdasd
Greenhorn
Posts: 5
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
SOLVED!
The problem was
Content-Lenght
. We must let URLConnection set that, instead of setting it manually.
So I removed
"_uc.setRequestProperty("Content-Length", Integer.toString(parameters.length()));"
and Fixed.
What does a metric clock look like? I bet it is nothing like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Telnet server / multiple readline(s)
Undefined index
how to solve arrayindexoutofbound exception
csv file is not created
error while connecting to webservices
More...