Android Question Intermittent timeout using PostString

achtrade

Active Member
Licensed User
Longtime User
Hi everybody,

I'm facing a problem with my connection. My app works fine, but suddenly, it starts with intermittent timeout using PostString after a couple minutes. My app executes a sp every 7 seconds, I'm not sure if this is the cause of my problem, if so, how can I fix it ?

I'm only using WIFI with 2 devices Galaxy Tab 7 & Galaxy S 3. I don't think that my server has any problem.

These are 2 errores that my app is showing:

error: org.apache.http.conn
ConnetTimeoutException: Connect to timed out

error: org.apache.http
NoHttpResponseexception: The target serverfailed to respond Job.

Need help

This is my relevant code:




B4X:
Sub Process_Globals
      Dim timer2 As Timer
      Private GET_MESSAGES = "get_messages"  As String  
End Sub

Sub Globals
      Dim SPResult As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
  timer2.Initialize("timer2",7000)
  timer2.Enabled=true
end sub
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
    Dim res As String
        res = Job.GetString
        Log("Response from server: " & res)
      
        Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
          
            Case GET_MESSAGES
                SPResult = parser.NextArray 'returns a list with maps
                 For i = 0 To SPResult.Size - 1
                    Dim M As Map
                    M = SPResult.get(i)
                    If M.get("id") < 0 Then
                        ToastMessageShow("xxx", True)
                    End If
                next
         End Select
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage & " Job.JobName=" & Job.JobName, True)
    End If
    Job.Release
end Sub


Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("http://www.mywebsite.com/myquery.php", Query)
End Sub
Sub GetMessages
    ExecuteRemoteQuery("CALL ListMessages", GET_MESSAGES)
End Sub

Sub timer2_Tick
      GetMessages
End Sub
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Hiya, try this sub.
Add the following sub in the post below into your project, call the sub at the beginning of Activity_Create.

https://www.b4x.com/android/forum/threads/workaround-the-networkonmainthread-exception.44760/

Let us know how you get on with the suggestion.

Another thing you can do is to teat for a valid internet connection before running your HTTP request. I always check for a valid internet connection using MLWiFi before sending any request to the internet. You never know, even when connected via WiFi or 4G...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not related to the network on main thread exception. HttpUtils2 handles all of the communication in the background.

You should expect some http requests to fail. There can be all kinds of reasons for failures. Based on the error it could be a server error.

Collect the failed requests and try to send them again after a few seconds.
 
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
This is not related to the network on main thread exception. HttpUtils2 handles all of the communication in the background.

You should expect some http requests to fail. There can be all kinds of reasons for failures. Based on the error it could be a server error.

Collect the failed requests and try to send them again after a few seconds.

it's handling well the failed request, it's trying again every 7 seconds, just I'm wonder why it's failing too much, I thought that my code had something wrong

something to do with this : job.GetRequest.Timeout = xxx ?

I'm using the HTTP lib v. 1.36, is that the right one ?

I've finally done a beautiful app and now this is happening to me
 
Last edited:
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
I had the exact same issue @achtrade, but now it's working. Let us know if you get it working.

Now it's snooker match time so I've got to go ;)

Ok, I went to the nearest McDonalds and there my app worked well in my two devices only with wifi. The strange thing is that in my office and my home I getting this behavior with the network. I would like to identify what the problem is ..... (scratching my head)
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Use MLWiFi to test for internet connection before doing anything else. If MLWiFi says there is an internet connection then process your HTTP request, if it says that it's not online then loop back and check again until it says that there is an internet connection.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Ok, I went to the nearest McDonalds and there my app worked well in my two devices only with wifi. The strange thing is that in my office and my home I getting this behavior with the network. I would like to identify what the problem is ..... (scratching my head)

Hi Achtrade, do you have identified the Problem ?
 
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
I didn't identify it, the issue persist in a random way, even in a McDonalds, I think that it is a Godaddy problem (my db is there), so, I'm changing everything to RDC with my own server.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Achtrade but in this way do you solved ? you've already done some testing ?
Thank
Marco
 
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
Achtrade but in this way do you solved ? you've already done some testing ?
Thank
Marco

Yes, It solved my problem, I tested it with the RDC sample and I put the same timer as in my app and leave it on for almost an hour, firing the ExecuteQuery every 7 seconds with no error connection.

Before, my app was getting error after 3 or 5 successful connections.
 
Upvote 0
Top