You could always use a java.net.Socket.

That's probably what I would do.



On Thursday, November 22, 2012 3:58:52 AM UTC-6, Ryan Bateman wrote:
>
> I'm attempting to write a simple API management class and running into an 
> issue when using HttpUrlConnection to POST content on a Galaxy Nexus 
> recently upgraded to 4.2. 
> In effect, attempting to post content fails on the first attempt with an 
> EOFException:
>
> java.io.EOFException
>> at libcore.io.Streams.readAsciiLine(Streams.java:203)
>> at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
>> at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
>> at 
>> libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
>> at 
>> libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
>> at com.murts.network.APIClient.post(APIClient.java:126)
>> at com.murts.network.APIManager$1.doInBackground(APIManager.java:49)
>
>
> The second attempt to post (with the same data) will work fine, as will 
> every alternate attempt to do so after that. 
>
> The code is as follows:
>
>                                 URL url = new URL(urlString);
>> connection = (HttpURLConnection) url.openConnection();
>> connection.setUseCaches(false);
>> connection.setDoOutput(true);
>> connection.setRequestProperty("Content-type", "application/json; 
>> charset=utf-8");
>> connection.setFixedLengthStreamingMode(postContent.getBytes().length);
>>
>> output = connection.getOutputStream();
>> output.write(postContent.getBytes());
>> if (connection.getResponseCode()  / 100 == 2) {
>> Ln.d("Seemed to work: " + connection.getResponseCode());
>> return new NetworkResponse(NetworkStatus.OKAY, 
>> connection.getResponseCode(), "");
>> } else {
>> String errorResponse = new 
>> String(readStream(connection.getErrorStream()));
>> Ln.e(TAG , "Error: " + errorResponse);
>>
>> String statusLine;
>> if (connection.getHeaderFields().get(null) != null && (statusLine = 
>> connection.getHeaderFields().get(null).get(0)) != null && 
>> TextUtils.split(statusLine, " ").length > 2) {
>> int potentialStatusCode = Integer.parseInt(TextUtils.split(statusLine, " 
>> ")[1]);
>> Ln.d("Parsed out empty response to reveal: " + potentialStatusCode);
>> return new NetworkResponse(NetworkStatus.OKAY, potentialStatusCode, "");
>> }
>> }
>> } catch (IOException e) {
>> Ln.e(e);
>> } finally {
>> if (connection != null) {
>> connection.disconnect();
>> }
>>                 }
>
>
> Testing on a 4.2 image on an emulator seems to work well, as does testing 
> on previous versions of Android on an emulator (2.3+).  
> I can't see any immediate issues with the code (it may looks hackish in 
> parts, but that's due more to being churned over thanks to endless 
> frustration with this issue) but perhaps I've missed something? 
> Anyone have any thoughts with this, on either a Galaxy Nexus in general or 
> with 4.2 specifically?
>
> Thanks in advance for any assistance (or even thoughts at all.)
>
> Ryan
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to