Android Question Post Json String with HttpJobs

catyinwong

Active Member
Licensed User
Longtime User
How to write it in B4a code with OKHttpUtils?


B4X: Copy
POST /post HTTP/1.1
Content-Type: application/json
X-Rapidapi-Host: theblock.ppapi.com
X-Rapidapi-Key: [YOUR API KEY]
Host: theblock.ppapi.com
Content-Length: 181

{
    "post_language_id": "22",
    "post_title": "My first post title",
    "post_content": "This is an example post. The post can be between three and 1000 characters long."
}
 
Check my code in zip file... search a little you ll find the way..here:


You must modify it with your parameters..
 
Upvote 0
Let me make my question clear a little bit. I want to write the code in B4a, using the httpJob. None of the following works. All returned with an error that post_language_id, post_title and post_content parameters are missing.

B4X: Copy
Dim j as httpJob
j.initialize("",me)

Dim url as string = "https://theblock.ppapi.com/example?post_language_id=22&post_title=My first post title&post_content=This is an example post. The post can be between three and 1000 characters long."
j.postString(url,"")
j.getRequest.SetHeader("X-Rapidapi-Host","theblock.ppapi.com")
j.getRequest.SetHeader("X-Rapidapi-Key","YOUR API KEY")


B4X: Copy
Dim j as httpJob
j.initialize("",me)

Dim url as string = "https://theblock.ppapi.com/example"
j.postString(url,"post_language_id=22&post_title=My first post title&post_content=This is an example post. The post can be between three and 1000 characters long.")
j.getRequest.SetHeader("X-Rapidapi-Host","theblock.ppapi.com")
j.getRequest.SetHeader("X-Rapidapi-Key","YOUR API KEY")

B4X: Copy
Dim j as httpJob
j.initialize("",me)

Dim url as string = "https://theblock.ppapi.com/example"
j.postmultipart(url,CreateMap("post_language_id":22,"post_title":"My first post title","post_content":"This is an example post. The post can be between three and 1000 characters long."),null)
j.getRequest.SetHeader("X-Rapidapi-Host","theblock.ppapi.com")
j.getRequest.SetHeader("X-Rapidapi-Key","YOUR API KEY")
 
Upvote 0
First of all you must generate the json right search how...

Then you must send it... I think is the same with b4j...
Code: Copy
    Dim JSON As JSONGenerator
    JSON.Initialize(Map1)
       
    Dim data As String =   JSON.ToPrettyString(1)
       
    Dim Job As HttpJob
    Job.Initialize("Job1",Me)
    Job.Username="..."
    Job.Password="..."
   
    Job.PostString("https://blah.com/name" , data )
    Job.GetRequest.SetContentType("application/json")
 
Upvote 0
First of all you must generate the json right search how...

Then you must send it... I think is the same with b4j...
Code: Copy
    Dim JSON As JSONGenerator
    JSON.Initialize(Map1)
      
    Dim data As String =   JSON.ToPrettyString(1)
      
    Dim Job As HttpJob
    Job.Initialize("Job1",Me)
    Job.Username="..."
    Job.Password="..."
  
    Job.PostString("https://blah.com/name" , data )
    Job.GetRequest.SetContentType("application/json")


Still return the same error response (parameters missing) with this:

B4X: Copy
Dim j as httpJob
j.initialize("",me)

Dim url as string = "https://theblock.ppapi.com/example"
Dim m as map = CreateMap("post_language_id":22,"post_title":"My first post title","post_content":"This is an example post. The post can be between three and 1000 characters long.")
Dim json as JSONGenerator

j.postString(url,json.ToPrettyString(1))
j.getRequest.SetHeader("X-Rapidapi-Host","theblock.ppapi.com")
j.getRequest.SetHeader("X-Rapidapi-Key","YOUR API KEY")
j.getRequest.SetContentType("application/json")
 
Upvote 0
Here is its original example code (Java OKHttp):

B4X: Copy
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\r
    \"post_language_id\": \"22\",\r
    \"post_title\": \"My first post title\",\r
    \"post_content\": \"This is an example post. The post can be between three and 1000 characters long.\"\r
}");
Request request = new Request.Builder()
    .url("https://theblock.ppapi.com/example")
    .post(body)
    .addHeader("content-type", "application/json")
    .addHeader("x-rapidapi-host", "https://theblock.ppapi.com")
    .addHeader("x-rapidapi-key", YOUR API KEY)
    .build();

Response response = client.newCall(request).execute();
 
Upvote 0
JSON.Initialize(m) ...where is it?

Solved! Thanks so much! I had the Json initialized in previous codes so the code runs with no error put it doesn't send the right string to http job. It works now! You saved my day! <3
 
Last edited:
Upvote 0
Back
Top