Android Question Posting JSON to Web API service

Russ

Member
Licensed User
Longtime User
Hello,

I'm really enjoying B4A, I've been more productive in a week than 3 years in eclipse

I'm missing how I use httpUtils2 to send a JSON string to my web service.

I have tested the service with REST Console for Chrome, the string that this code produces works when sent as an application/json Content-Type.

This is the code that returns an Internal Server Error.

B4X:
Sub PerformUpload ()
    Dim mylist As List
    mylist.Initialize
    Dim m As Map
   
    Dim Cursor1 As Cursor
    Cursor1 = SQLObj.ExecQuery("SELECT name FROM people")
    For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i
        m.Initialize
        m.Put("name",Cursor1.GetString("name"))
        mylist.Add(m)
    Next
    Cursor1.Close
   
    Dim j As JSONGenerator
    j.Initialize2(mylist)
    Msgbox(j.ToPrettyString(2), "")
   
    'Send a POST request - this is where I'm struggling
    Dim job As HttpJob
    job.Initialize("SendAssets", Me)
    job.PostString("http://myserver/servicething/api/People", j.ToString)
   
    ToastMessageShow("Sending...", True)
End Sub

Also if there is a more elegant way to produce the JSON I'd like to hear it

Thank you

Russ
 

eps

Expert
Licensed User
Longtime User
Have you tried getting it to work with a hardcoded string first? I would check what j.String is actually returning as well..
 
Upvote 0

Russ

Member
Licensed User
Longtime User
I'm now trying to hack about httpUtils2, I have set

req.SetContentType("application/json")

in the PostBytes sub, but I get

java.lang.RuntimeException: Only Post / Put requests support this method.

I thought that was a post request?

eps: Yes the string is ok, a few lines up I have Msgbox(j.ToPrettyString(2), "") to show what I'm about to send. Do you think my way should work?

Thank you both for your help
 
Upvote 0

Reviewnow

Active Member
Licensed User
Longtime User
Download the dbutils class and try the following

B4X:
 Sub PerformUpload ()
          'Used from the dbutils tutorial modifications may be required
          Dim j As JSONGenerator
          j.Initialize(DBUtils.ExecuteJSON(SQLObj, "SELECT name FROM people", Null, _
              0, Array As String(DBUtils.DB_TEXT, DBUtils.DB_TEXT, DBUtils.DB_INTEGER)))
          Dim jString As String
          jString = j.ToPrettyString(4)
          Log(jString)
   
     
        'Send a POST request - this is where I'm struggling
        Dim job As HttpJob
        job.Initialize("SendAssets", Me)
       
         'if your web service requires a parameter then include it
         'job.PostString("...", "People=" & jString)

        job.PostString("http://myserver/servicething/api/People", jString)
        'include the contentType
        job.SetContentType("application/json")
     
        ToastMessageShow("Sending...", True)
End Sub
 
Upvote 0

Russ

Member
Licensed User
Longtime User
Thank you reviewnow. That's a much tidier looking JSON generator

SetContentType isn't a member of HttpJob, to get around this I have tried creating my own PostBytes for JSON

B4X:
Public Sub PostJSON(Link As String, Text As String)
    req.SetContentType("application/json")
    req.InitializePost2(Link, Text.GetBytes("UTF8"))
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

But this errors with

B4X:
Error occurred on line: 31 (httpjob)
java.lang.RuntimeException: Only Post / Put requests support this method.
    at anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper.SetContentType(HttpClientWrapper.java:426)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    etc ....

I'll dig more around the DBUtils class see what I can find
 
Upvote 0

Russ

Member
Licensed User
Longtime User
I solved it

B4X:
Public Sub PostJSON(Link As String, Text As String)
    req.InitializePost2(Link, Text.GetBytes("UTF8"))
    req.SetContentType("application/json")
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

Note the SetContentType is now after the InitializePost2. Pop that in your HttpJob class module. It was solved with thanks to this thread, regarding JSON GetRequest

http://www.b4x.com/android/forum/threads/httputils2service-bas-and-application-json.19738/

I'll probably be back in 10 minutes with another difficulty

Thanks again
 
Reactions: eps
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…