Android Question Traslate field Java to B4A

MarcoRome

Expert
Licensed User
Hi all.
I have this code in java:

B4X:
'HttpResponse<JsonNode> response = Unirest.post("https://xxxxxx/xxx-validate")
'.header("X-Mashape-Key", "7ikZQqUI6Fmsh5SwxxxqiwToAEUp1J16Cojsnf7nqE2KYfsqu")
'.header("Content-Type", "application/x-www-form-urlencoded")
'.header("Accept", "application/json")
'.field("number", "1-888-322-113333")
'.asJson();
translate in B4A is:

B4X:
    Dim job1 As HttpJob
    job1.Initialize("MyJob", Me)
    job1.Download("https://xxxxxx/xxx-validate")
    job1.GetRequest.SetHeader("X-Mashape-Key", "7ikZQqUI6Fmsh5Sw2lQfqiwxxx1J16Cojsnf7nqE2KYfsqu")
    job1.GetRequest.SetHeader("Content-Type", "application/json")
    job1.GetRequest.SetContentEncoding("text/plain")
but this line

B4X:
'.field("number", "1-888-322-113333")
as it is translated in B4A ??

Thank you
Marco
 

DonManfred

Expert
Licensed User
i would try it this way (dont know if this works

B4X:
    Dim job1 As HttpJob
  job1.Initialize("MyJob", Me)
  job1.Download("https://xxxxxx/xxx-validate")
    job1.PostString("https://xxxxxx/xxx-validate",$"{"number":"1-888-322-113333"}"$)
  job1.GetRequest.SetHeader("X-Mashape-Key", "7ikZQqUI6Fmsh5Sw2lQfqiwxxx1J16Cojsnf7nqE2KYfsqu")
  job1.GetRequest.SetHeader("Content-Type", "application/json")
  job1.GetRequest.SetContentEncoding("text/plain")
 

MarcoRome

Expert
Licensed User
i would try it this way (dont know if this works

B4X:
    Dim job1 As HttpJob
  job1.Initialize("MyJob", Me)
  job1.Download("https://xxxxxx/xxx-validate")
    job1.PostString("https://xxxxxx/xxx-validate",$"{"number":"1-888-322-113333"}"$)
  job1.GetRequest.SetHeader("X-Mashape-Key", "7ikZQqUI6Fmsh5Sw2lQfqiwxxx1J16Cojsnf7nqE2KYfsqu")
  job1.GetRequest.SetHeader("Content-Type", "application/json")
  job1.GetRequest.SetContentEncoding("text/plain")
Thank DonMandred but dont work, the message is:
B4X:
Response from server: {"api-parameter-name":"number","api-error":1,"api-parameter-type":"string","api-parameter-required":true,"api-error-msg":"MISSING OR INVALID PARAMETER"}
in attachemnt example, so if you want try.
Thank you again for your response.

This is SITE for this service
 

Attachments

MarcoRome

Expert
Licensed User
This is PHP Code:

B4X:
// These code snippets use an open-source library. http://unirest.io/php
$response = Unirest\Request::post("https://neutrinoapi-phone-validate.p.mashape.com/phone-validate",
  array(
    "X-Mashape-Key" => "7ikZQqUI6Fmsh5Sw2lQfqiwToAEUp1J16Cojsnf7nqE2KYfsqu",
    "Content-Type" => "application/x-www-form-urlencoded",
    "Accept" => "application/json"
  ),
  array(
    "number" => "1-888-322-1133338"
  )
);
This is Java Code:

B4X:
// These code snippets use an open-source library. http://unirest.io/java
HttpResponse<JsonNode> response = Unirest.post("https://neutrinoapi-phone-validate.p.mashape.com/phone-validate")
.header("X-Mashape-Key", "7ikZQqUI6Fmsh5Sw2lQfqiwToAEUp1J16Cojsnf7nqE2KYfsqu")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept", "application/json")
.field("number", "1-888-322-1133338")
.asJson();
This is Curl:

B4X:
curl -X POST --include 'https://neutrinoapi-phone-validate.p.mashape.com/phone-validate' \
  -H 'X-Mashape-Key: 7ikZQqUI6Fmsh5Sw2lQfqiwToAEUp1J16Cojsnf7nqE2KYfsqu' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Accept: application/json' \
  -d 'number=1-888-322-1133338'
 

DonManfred

Expert
Licensed User
If i look at this site there should be a country-code too

B4X:
curl -X POST --include 'https://neutrinoapi-phone-validate.p.mashape.com/phone-validate' \
  -H 'X-Mashape-Key: <required>' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Accept: application/json' \
  -d 'country-code=GB' \
  -d 'number=+447522123456'
B4X:
Dim job1 As HttpJob
  job1.Initialize("MyJob", Me)
  job1.Download("https://xxxxxx/xxx-validate")
    job1.PostString("https://xxxxxx/xxx-validate",$"{"number":"1-888-322-113333","country-code","GB"}"$)
  job1.GetRequest.SetHeader("X-Mashape-Key", "7ikZQqUI6Fmsh5Sw2lQfqiwxxx1J16Cojsnf7nqE2KYfsqu")
  job1.GetRequest.SetHeader("Content-Type", "application/json")
  job1.GetRequest.SetContentEncoding("text/plain")
maybe?
 

MarcoRome

Expert
Licensed User
If i look at this site there should be a country-code too

B4X:
curl -X POST --include 'https://neutrinoapi-phone-validate.p.mashape.com/phone-validate' \
  -H 'X-Mashape-Key: <required>' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Accept: application/json' \
  -d 'country-code=GB' \
  -d 'number=+447522123456'
B4X:
Dim job1 As HttpJob
  job1.Initialize("MyJob", Me)
  job1.Download("https://xxxxxx/xxx-validate")
    job1.PostString("https://xxxxxx/xxx-validate",$"{"number":"1-888-322-113333","country-code","GB"}"$)
  job1.GetRequest.SetHeader("X-Mashape-Key", "7ikZQqUI6Fmsh5Sw2lQfqiwxxx1J16Cojsnf7nqE2KYfsqu")
  job1.GetRequest.SetHeader("Content-Type", "application/json")
  job1.GetRequest.SetContentEncoding("text/plain")
maybe?
Yes but this field is optional
 

MarcoRome

Expert
Licensed User
In this mode work ( without $ ):

B4X:
job1.PostString("https://neutrinoapi-phone-validate.p.mashape.com/phone-validate","number=1-888-322-113333")
Thank you for your support DonManfred ;)
 
Top