Can't get a response with HttpUtils2 trying to consume a RESTFUL webservice

jlperez

Member
Licensed User
Longtime User
Dear guys,
first of all thank you for building such a great product.

Great help for all of us coming from VB.NET world.

I'm rewriting the way I was consuming Webservices to avoid main on thread exception so I'm trying to use HttpUtils2.

I'm not able to get any output to webservice I'm using to send data coming from MySQL DB.

You may test webservice here:
http://fieldservice.the-mobility-group.com/webservices/descarga.php?tabla=Tipo_Usuario&format=json

This is the way I'm calling webservice

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim job As HttpJob
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.
   Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
 job.Initialize("Sincronizar",Me)
 BaseDatos.Inicializacion_DB(Main.bdbsql,"FieldService.db")   
 ProgressDialogShow("Cargando datos ...")
 Sincronizar_TablasDescarga
 ProgressDialogHide
End Sub
And these are the subs where work is taking place

B4X:
Sub DescargarDatos(tabla As String)
   Main.tablaComunicaciones = tabla
   'job.Initialize("Sincronizar",Me)
   job.PostString(Main.url, "?tabla=" & tabla & "&" & "format=json")
End Sub
Sub Sincronizar_TablasDescarga
 DescargarDatos("Tipo_Usuario")
 DescargarDatos("Usuario")
End Sub

I'm using JobDone example you gave!
B4X:
Sub JobDone (pJob As HttpJob)
Dim result As String
    Log("JobName = " & pJob.JobName & ", Success = " & pJob.Success)
    If pJob.Success = True Then
        Select pJob.JobName
            Case "Sincronizar"
                ' Procesamos los resultados
            result = pJob.GetString
            Log("Salida:" & result)
            ParsearDatosTablas(result)            
            Case "Job3"
                Log(pJob.GetString)
        End Select
    Else
        Log("Error: " & pJob.ErrorMessage)
        ToastMessageShow("Error: " & pJob.ErrorMessage, True)
    End If
    pJob.Release
End Sub

Crash is giving an error with JSON parsing but this is not important since parsing works, the problem is trying to parse a void string.

B4X:
** Activity (main) Pause, UserClosed = true **
** Activity (login) Create, isFirst = true **
** Activity (login) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
JobName = Sincronizar, Success = true
Salida: 
JobName = Sincronizar, Success = true
Salida: 
login_parseardatostablas (B4A line: 123)
nivelTop = parser.NextObject
org.json.JSONException: End of input at character 2 of  
   at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
   at org.json.JSONTokener.nextValue(JSONTokener.java:97)
   at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:45)
   at tmg.fieldservice.login._parseardatostablas(login.java:468)
   at tmg.fieldservice.login._jobdone(login.java:401)
   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.keywords.Common$4.run(Common.java:884)
   at android.os.Handler.handleCallback(Handler.java:605)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:211)
   at anywheresoftware.b4a.Msgbox.debugWait(Msgbox.java:156)
   at anywheresoftware.b4a.debug.Debug.wait(Debug.java:210)
   at anywheresoftware.b4a.debug.Debug.reachBP(Debug.java:257)
   at anywheresoftware.b4a.debug.Debug.ErrorCaught(Debug.java:146)
   at tmg.fieldservice.login._parseardatostablas(login.java:602)

Do you have any clue?

Thank you in advance for your help.

Best regards
Jose Luis
 

jlperez

Member
Licensed User
Longtime User
Thanks !!

Thank you Erel!
You're right. ;)

I've looked to webservice code... !!

B4X:
if(isset($_GET['tabla']) ) {

  $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
  $tabla_nom = $_GET['tabla']; 
  $fecha_sinc = $_GET['ult_fecha'];

Best regards
Jose Luis
 
Upvote 0

jlperez

Member
Licensed User
Longtime User
Slight correction

You can see that the string returned is empty.

I guess that your server expects a GET request and not a POST request:
B4X:
job.Download(Main.url, Array As String("tabla",tabla, "format","json"))

It has to be Download2
 
Upvote 0

jlperez

Member
Licensed User
Longtime User
Multiple Calls

I'm on the same problem that http://www.b4x.com/forum/basic4andr...16388-httputils-multiple-calls.html#post93302


I have to sync a series of MySQL tables with SQL Lite and I'm using a very light rest webservice to do it.

The only workarround I'm think is use JobDone as a Controller authorizing next call once is done and success but this is not very clean.

Is there anything more smart to achieve the same?

Thank you in advance.

Best regards
Jose Luis
 
Upvote 0

jlperez

Member
Licensed User
Longtime User
Almost Solved

I'm on the same problem that http://www.b4x.com/forum/basic4andr...16388-httputils-multiple-calls.html#post93302


I have to sync a series of MySQL tables with SQL Lite and I'm using a very light rest webservice to do it.

The only workarround I'm think is use JobDone as a Controller authorizing next call once is done and success but this is not very clean.

Is there anything more smart to achieve the same?

Thank you in advance.

Best regards
Jose Luis


As I saw in other post works by building a List of jobs insted of doing some kind of loop.

Now I'm dealing with some issue of special spanish characters like Ñ and accents.
 
Upvote 0

jlperez

Member
Licensed User
Longtime User
Special characters

The problem with spanish special characters doesn't have to be with Android neither B4A.

It's on the webservice but I don't know why it's happening because theorically database encoding is UTF8 as well as json encoding.

I'll post details when I clarify this.
 
Upvote 0

jlperez

Member
Licensed User
Longtime User
Solved json_encode returning null for special characters

The problem with spanish special characters doesn't have to be with Android neither B4A.

It's on the webservice but I don't know why it's happening because theorically database encoding is UTF8 as well as json encoding.

I'll post details when I clarify this.

If anybody else is suffering from this problem may use the trick I did...

mysql_query('SET CHARACTER SET utf8');

just after connecting to de database, this is the way to guarantee database results will be utf-8 encoded.

Hope It helps.

Best regards
Jose Luis
 
Upvote 0
Top