Android Question httputils2 strange behavour

jayel

Active Member
Licensed User
Hello,

When I use the httputils2 to send and receive data from my testserver local, everything works.
I uploafd my php file to my webhosting and the same code gives me an error on decoding Json :
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Activity (main) Resume **
JobName = LoginSpecial, Success = true
{"tag":"getcountwerkengepauzeerd","error":false,"result":7}
Error occurred on line: 323 (main)
java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:50)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA$3.run(BA.java:332)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Can somebowy help me ?
 

jayel

Active Member
Licensed User
B4X:
Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  If Job.Success = True Then
      Select Job.JobName
       
        Case "getcountwerkengepauzeerd"
            Dim mystr As String
            mystr = Job.GetString
            If mystr <> "" Then
                CheckJSON(mystr,Job.JobName)
               
            End If
           
        Case "getcountgeplandewerken"
            Dim mystr As String
            mystr = Job.GetString
            If mystr <> "" Then
                CheckJSON(mystr, Job.JobName)
               
            End If
        
      End Select
  Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
  End If
  Job.Release
End Sub
CheckJson
B4X:
Sub CheckJSON(AantalSTR As String, jobname As String )
    Log(AantalSTR)
   
    Dim myJson As JSONParser

    myJson.Initialize(AantalSTR)
    Dim map1 As Map
    map1 = myJson.nextobject '<--- error here
    Dim errorstr As String
    errorstr = map1.Get("error")
    Dim tag As String
    tag = map1.Get("tag")
   
    If errorstr = "false" Then
        Dim aantal As String
        aantal = map1.Get("result")
        Select Case jobname
            Case "getcountwerkengepauzeerd"
                UpdateBadgeWerkenGepauzeerd(aantal)
                GetGeplandeWerken
            Case "getcountgeplandewerken"
                UpdateBadgeGeplandeWerken(aantal)
                ProgressDialogHide
               
        End Select
       
'        If aantal <> "0" Then
'            UpdateBadgeWerkenGepauzeerd(aantal)
'        End If
    Else
        Dim myerrormsg As String
        myerrormsg = map1.Get("error_msg")
        ToastMessageShow(myerrormsg, True)
       
   
    End If
   
   
End Sub
 

jayel

Active Member
Licensed User
No, it give the same error.

B4X:
    Dim parser As JSONParser
    parser.Initialize(AantalSTR)
    Dim root As Map = parser.NextObject '<-- error here     
    Dim error As String = root.Get("error")
    Dim tag As String = root.Get("tag")
    Dim result As Int = root.Get("result")
{"tag":"getcountwerkengepauzeerd","error":false,"result":7}
Error occurred on line: 202 (main)
java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:50)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA$3.run(BA.java:332)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
 

jayel

Active Member
Licensed User
Hello,

Just made a small example, can I send you a mail with my testproject?
 

jayel

Active Member
Licensed User
I have send you a small project.
Could you confirm that you received the file?
 

jayel

Active Member
Licensed User
Hello,

So after a whole day trying with Manfred (many thanks), we didn't find the solution.
on my hosting the return Json was rapped arround a <pre> tag.
HTML:
<pre style="word-wrap: break-word; white-space: pre-wrap;">{"tag":"getcountwerkengepauzeerd","error":false,"result":7}</pre>
I wanted just the json....
I managed to remove the <pre> tag with this on the top of my php code :
PHP:
header('Content-Type: text/html')
Now I don't have the <pre> tag but 3 empty chars on the beginning of my script.
When I remove the empty chars my output works, but that is not the way to go....

What am i missing here ??
 

jayel

Active Member
Licensed User
Hello,

Just to avoid that somebody else has the same issue.
The 3 characters in the beginning are ascii = 65279
These are BOM characters.
I was using a php extension for visual studio to write my php code, now it seems that when you use include or require it put a BOM character.
When I use Notepad++ then I can save UTF8 without BOM and everything works.
see : http://stackoverflow.com/questions/41647/php-include-function-outputting-unknown-char

Many thanks to Manfred for the help !
 
Top