Android Question implement restful webservice

leandroavila74

Member
Licensed User
Longtime User
how would this code in java written in b4a?
Code: Copy
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "coordenadas=%7B%20%20%0A%20%20%20%22imei%22%3A%22354140091389995%22%2C%0A%20%20%20%22cd_usuario%22%3A%22926.800.970-68%22%2C%0A%20%20%20%22coordenadas%22%3A%5B%20%20%0A%20%20%20%20%20%20%7B%20%20%0A%20%20%20%20%20%20%20%20%20%22cpf%22%3A%22926.800.970-68%22%2C%0A%20%20%20%20%20%20%20%20%20%22guid%22%3A%22guid0%22%2C%0A%20%20%20%20%20%20%20%20%20%22data_hora%22%3A%222019-02-13%2013%3A26%3A02%22%2C%0A%20%20%20%20%20%20%20%20%20%22latitude%22%3A%22-29.9254565%22%2C%0A%20%20%20%20%20%20%20%20%20%22longitude%22%3A%22-51.1448786%22%2C%0A%20%20%20%20%20%20%20%20%20%22bateria%22%3A%220%22%2C%0A%20%20%20%20%20%20%20%20%20%22velocidade%22%3A%220%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%5D%0A%7D&undefined=");
Request request = new Request.Builder()
  .url("http://www.modular.com.br/desenvolvimento/entregarwebservice/v4/posicao/")
  .put(body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .addHeader("Authorization", "Basic OTI2LjgwMC45NzAtNjg6MDEwMQ==")
  .addHeader("cache-control", "no-cache")
  .addHeader("Postman-Token", "ff13a900-c990-48b3-84ee-a55bd86d9258")
  .build();

Response response = client.newCall(request).execute();
 
Search for REST Examples using okhttputils2. I´m pretty sure there are SOME.
 
Upvote 0
how would this code in java written in b4a?
Code: Copy
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "coordenadas=%7B%20%20%0A%20%20%20%22imei%22%3A%22354140091389995%22%2C%0A%20%20%20%22cd_usuario%22%3A%22926.800.970-68%22%2C%0A%20%20%20%22coordenadas%22%3A%5B%20%20%0A%20%20%20%20%20%20%7B%20%20%0A%20%20%20%20%20%20%20%20%20%22cpf%22%3A%22926.800.970-68%22%2C%0A%20%20%20%20%20%20%20%20%20%22guid%22%3A%22guid0%22%2C%0A%20%20%20%20%20%20%20%20%20%22data_hora%22%3A%222019-02-13%2013%3A26%3A02%22%2C%0A%20%20%20%20%20%20%20%20%20%22latitude%22%3A%22-29.9254565%22%2C%0A%20%20%20%20%20%20%20%20%20%22longitude%22%3A%22-51.1448786%22%2C%0A%20%20%20%20%20%20%20%20%20%22bateria%22%3A%220%22%2C%0A%20%20%20%20%20%20%20%20%20%22velocidade%22%3A%220%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%5D%0A%7D&undefined=");
Request request = new Request.Builder()
  .url("http://www.modular.com.br/desenvolvimento/entregarwebservice/v4/posicao/")
  .put(body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .addHeader("Authorization", "Basic OTI2LjgwMC45NzAtNjg6MDEwMQ==")
  .addHeader("cache-control", "no-cache")
  .addHeader("Postman-Token", "ff13a900-c990-48b3-84ee-a55bd86d9258")
  .build();

Response response = client.newCall(request).execute();
you know if the request is a POST,GET,PUT?


Code: Copy
    Private body As String = "coordenadas=%7B%20%20%0A%20%20%20%22imei%22%3A%22354140091389995%22%2C%0A%20%20%20%22cd_usuario%22%3A%22926.800.970-68%22%2C%0A%20%20%20%22coordenadas%22%3A%5B%20%20%0A%20%20%20%20%20%20%7B%20%20%0A%20%20%20%20%20%20%20%20%20%22cpf%22%3A%22926.800.970-68%22%2C%0A%20%20%20%20%20%20%20%20%20%22guid%22%3A%22guid0%22%2C%0A%20%20%20%20%20%20%20%20%20%22data_hora%22%3A%222019-02-13%2013%3A26%3A02%22%2C%0A%20%20%20%20%20%20%20%20%20%22latitude%22%3A%22-29.9254565%22%2C%0A%20%20%20%20%20%20%20%20%20%22longitude%22%3A%22-51.1448786%22%2C%0A%20%20%20%20%20%20%20%20%20%22bateria%22%3A%220%22%2C%0A%20%20%20%20%20%20%20%20%20%22velocidade%22%3A%220%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%5D%0A%7D&undefined="
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("http://www.modular.com.br/desenvolvimento/entregarwebservice/v4/posicao/",body)
    j.GetRequest.SetContentType("Content-Type", "application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Authorization", "Basic OTI2LjgwMC45NzAtNjg6MDEwMQ==")
    j.GetRequest.SetHeader("cache-control", "no-cache")
    j.GetRequest.SetHeader("Postman-Token", "ff13a900-c990-48b3-84ee-a55bd86d9258")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    Else
        Log(j.ErrorMessage)
    End If
    j.Release

you can try something like this, with libs okhttp and okhttputils
if dont work with post, try change to j.PutString ....
 
Upvote 0
you know if the request is a POST,GET,PUT?


Code: Copy
    Private body As String = "coordenadas=%7B%20%20%0A%20%20%20%22imei%22%3A%22354140091389995%22%2C%0A%20%20%20%22cd_usuario%22%3A%22926.800.970-68%22%2C%0A%20%20%20%22coordenadas%22%3A%5B%20%20%0A%20%20%20%20%20%20%7B%20%20%0A%20%20%20%20%20%20%20%20%20%22cpf%22%3A%22926.800.970-68%22%2C%0A%20%20%20%20%20%20%20%20%20%22guid%22%3A%22guid0%22%2C%0A%20%20%20%20%20%20%20%20%20%22data_hora%22%3A%222019-02-13%2013%3A26%3A02%22%2C%0A%20%20%20%20%20%20%20%20%20%22latitude%22%3A%22-29.9254565%22%2C%0A%20%20%20%20%20%20%20%20%20%22longitude%22%3A%22-51.1448786%22%2C%0A%20%20%20%20%20%20%20%20%20%22bateria%22%3A%220%22%2C%0A%20%20%20%20%20%20%20%20%20%22velocidade%22%3A%220%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%5D%0A%7D&undefined="
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("http://www.modular.com.br/desenvolvimento/entregarwebservice/v4/posicao/",body)
    j.GetRequest.SetContentType("Content-Type", "application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded")
    j.GetRequest.SetHeader("Authorization", "Basic OTI2LjgwMC45NzAtNjg6MDEwMQ==")
    j.GetRequest.SetHeader("cache-control", "no-cache")
    j.GetRequest.SetHeader("Postman-Token", "ff13a900-c990-48b3-84ee-a55bd86d9258")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    Else
        Log(j.ErrorMessage)
    End If
    j.Release

you can try something like this, with libs okhttp and okhttputils
if dont work with post, try change to j.PutString ....


Thanks a lot for the help
I needed the put method.
but the example you provided is enough to complete my task now

once again thank you very much for making your valuable time available to help someone who is returning to work with this fantastic tool

(Obrigado pela ajuda amigo!)
 
Upvote 0
Back
Top