Android Question WebService Rest error - Please Help!

joaquinortiz

Active Member
Licensed User
Longtime User
Hello Amigos!

I test the sample attached in this post https://www.b4x.com/android/forum/threads/webservice-rest.73466/#post-466819 from @DonManfred with my webservice. I make a very simple Web Api web service. and it doesn't works. Can anyone help me with this?

B4X:
#Region  Project Attributes
    #ApplicationLabel: RestTest
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    
    Dim Register As HttpJob
    Register.Initialize("rest", Me)
'    Register.Tag = "resttest.json"
'  Register.Download("http://resttst.yaskawa.com.br:9090/rest/sample")
    Register.Download("http://localhost/miws/api/Licencia/5")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
  If Job.Success Then
        If Job.JobName = "rest" Then
            'Dim out As OutputStream
            'out = File.OpenOutput(File.DirRootExternal,Job.Tag,False )
            'File.Copy2(Job.GetInputStream, out)
            'out.Close
            Log($"The job resttest finished "$)
            
            ParseJSON(Job.GetString)
            
        End If
  Else
      Log( Job.ErrorMessage)
  End If
    Job.Release
End Sub
Sub ParseJSON(jsonstring As String)
    Dim parser As JSONParser
    parser.Initialize(jsonstring)
    Dim root As List = parser.NextArray
    For Each colroot As Map In root
         Dim name As String = colroot.Get("name")
         Dim id As Int = colroot.Get("id")
        Log($"${name} - ${id}"$)
    Next
End Sub
 

Attachments

  • WebApi Error1.png
    WebApi Error1.png
    218.3 KB · Views: 173
  • WebApi Error2.png
    WebApi Error2.png
    6 KB · Views: 171

drgottjr

Expert
Licensed User
Longtime User
sooner or later somebody will tell you that JobDone isn't used anymore. use Wait For (you can search for that in the box above). but that's not's causing the error.

as for error1, the only way you can use localhost is if the server is on the same device as the caller. i'm guessing you have a server running on a desktop, and you want to connect to it from an android device. localhost for the android device is the android device. localhost for the desktop is the desktop. to connect to a server on a desktop from an android device, you need to use the desktop's ip address (presumably, something like 192.168,1.3)

as for error 2, i don't see what the error is...
 
Upvote 0

joaquinortiz

Active Member
Licensed User
Longtime User
as for error1, the only way you can use localhost is if the server is on the same device as the caller. i'm guessing you have a server running on a desktop, and you want to connect to it from an android device. localhost for the android device is the android device. localhost for the desktop is the desktop. to connect to a server on a desktop from an android device, you need to use the desktop's ip address (presumably, something like 192.168,1.3)

Thanks @drgottjr . I'm new developing Webservices. Thanks for clarifications!. Let me search for samples using "Wait For". Let me change it with the IP address and test it.

as for error 2, i don't see what the error is...
Sorry, I just want to share that my web api is working good.
 
Upvote 0

joaquinortiz

Active Member
Licensed User
Longtime User
@drgottjr I use the following sample and it doesn't works....it show me this error.

Logger connected to: motorola Moto G Play
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
ResponseError. Reason: java.net.SocketTimeoutException: connect timed out, Response:
Error: java.net.SocketTimeoutException: connect timed out


B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
'    xui.MsgboxAsync("Hello world!", "B4X")
    Dim j As HttpJob
    j.Initialize("",Me)
'    j.Download("https://api.coindesk.com/v1/bpi/currentprice.json")
    j.Download("http://192.168.1.68/miws/api/Licencia/5")
    wait for (j) jobdone (j As HttpJob)
    If j.Success Then
            Log(j.GetString)
        Else
            Log("Error: " & j.ErrorMessage)
    End If
    j.Release
End Sub
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
so, i'm guessing you tried connecting with the coindesk.com server, and it was ok, yes?

the timeout error means the server is not responding. (stupid question: was the server running when you did the test?)

are you sure 192.168.1.68 is the server's address? are you sure your android device is on the same subnet (or that there is some way to route the connection)?

if your device is on cellular data (not wifi), you're not going to be able to connect. also, if your device is plugged into the desktop via usb, but doesn't have networking turned on (wifi)

is the server listening on port 80? unless you configured it otherwise, it should be on port 80 by default

do you have a firewall on your router that does not allow any incoming connections? you should make sure whichever port the server is listening on is open.
a typical firewall configuration will not allow incoming connections

how many nodes are on your network anyway? .68 is a little odd for a home network. if you actually have a number of computers on the network, can you
connect to .68 from any of them?

last, i'm wondering why you don't see another error when trying to connect to http (not https). the error should say something about "clear text". did you edit your manifest to allow http connections? the clear text error shouldn't be related to the timeout, but let's see what you determine when answering my questions.
 
Last edited:
Upvote 0

joaquinortiz

Active Member
Licensed User
Longtime User
Greaaattt!!!! @drgottjr It works!!!!!!!!!!!!!!.

Everything was ok, except my firewall. I have to turn it off!.

Thanks very much for your time!!. I started to get frustrated because everything I read I implemented it and it did not work!. =D.

Now, let me dive more into this webservice data manipulation between my device and my server.

Have a good nigth. Greetings from Mexico. Gracias amigo!.
 
Upvote 0
Top