Strange error with HttpClient

susu

Well-Known Member
Licensed User
Longtime User
Hi guys,
My app reads html code from a website to extract useful data. It worked on my phone with Android 2.3.6 but got this error on another phone with Android 4.0.4

player_hc_responsesuccess (java line: XXX)
java.net.SocketException: recvfrom failed:
ECONNRESET (Connection reset by peer)

It's strange because when I changed to another internet connection (via wifi) the error never occurred. When I changed back, it got error again. :BangHead: Can you explain to me? Thank you so much.
 

susu

Well-Known Member
Licensed User
Longtime User
I tried my app on 2 phones use the same network. A phone with Android 2.3.6 worked, a phone with Android 4.0.4 didn't work.

I googled and found answer from stackoverflow:
This behavior can be disabled by setting the http.keepAlive system property to false before issuing any HTTP requests
Link

How to do this?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
The Http library is based on Apache HttpClient. The suggestion in this link is for a different API. Are you calling Request.Release when your done with it?

Did you mean Response.Release? Because I can't find Request.Release
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
I tried to Response.Release before calling new request but it didn't help. Here the error from Log Cat:

channel_hc_responsesuccess (java line: XXXX)
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:542)
at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159)
at java.io.InputStreamReader.read(InputStreamReader.java:244)
at java.io.Reader.read(Reader.java:145)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:135)
at anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper.GetString(HttpClientWrapper.java:471)
at com.myapp.channel._hc_responsesuccess(channel.java:1021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
at anywheresoftware.b4a.BA$3.run(BA.java:301)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:131)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
So how I can get the html code from a website? Do you suggest another way?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
I don't think the site got problem because on Android 2.3.6 I never see this error.
I will try HttpUtil2.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
I used HttpUtils2 to get html code but it seems never response. In Log Cat it only show:
sending message to waiting queue of uninitialized activity (submitjob)

I'm sure the website is only because it's Google.

My code:

B4X:
Sub RequestSource
   ProgressDialogShow2("Please wait...", True)
   Dim job1 As HttpJob
   job1.Initialize("Job1", Me)
   job1.Download("http://www.google.com")
End Sub

Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
      Select Job.JobName
            Case "Job1"
              Msgbox(Job.GetString, "Source")
         End Select
     Else
      ToastMessageShow("Can not connect to server", True)
   End If
   Job.Release
End Sub

:BangHead::BangHead::BangHead::BangHead:
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Hi Susu,

don't know if it is of importance or related but can Msgbox (being modal) be used in your above routine? Try with log or toast to see if the code works.

Just a guess since I cannot try in this moment.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Hi Moster67,
I used Msgbox to make sure this code work. It's really strange because I cut this code to create new project then it worked but in my old project it didn't. I really don't know what happen.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
I still stuck with this problem...
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Hi Erel, what exactly this message means?

sending message to waiting queue of uninitialized activity (submitjob)

The code below worked fine in sample project but in my big project I got this message. How to fix/change it?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Are you destroying HttpUtils2Service?

No, and I don't know how to destroy HttpUtils2Service.
I added 2 modules HttpJob.bas & HttpUtils2Service.bas into my old project. I edited my code to use HttpUtils2 but I got this message. I don't know how to deal with it. May I send you my project to take a look?
 
Upvote 0
Top