B4J Question rest api

spiroskaras

Member
Licensed User
Longtime User
Can you help me to connect to rest api of an organisation with b4j ?
I have user name and api key.The sample for connection is in java code.


sample code:

import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class JavaSample
{
public static void main(String[] args)
{
HttpClient httpclient = HttpClients.createDefault();

try
{
URIBuilder builder = new URIBuilder("https://mydata-dev.azure-api.net/RequestDocs");

builder.setParameter("mark", "{mark}");

URI uri = builder.build();
HttpGet request = new HttpGet(uri);
request.setHeader("aade-user-id", "{aade-user-id}");
request.setHeader("Ocp-Apim-Subscription-Key", "{subscription key}");


// Request body
StringEntity reqEntity = new StringEntity("{body}");
request.setEntity(reqEntity);

HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();

if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Looks like an error:
B4X:
StringEntity reqEntity = new StringEntity("{body}");
request.setEntity(reqEntity);
GET requests shouldn't have a body.

Tutorial: https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/

Something like:
B4X:
Dim j As HttpJob
j.Initialize(Me, "")
j.Download("https://...")
j.GetRequest.SetHeader(...)
j.GetRequest.SetHeader(...)
Wait For (j) JobDone (j As HttpJob)
...
 
Upvote 0
Top