herbertxm herbertxm - 3 months ago 12
Android Question

How could I create the same request the curl example does (JAVA ANDROID)

I want to know how to convert the example in CURL to JSONObject. I have tried many examples on the internet, but I didn't figure out how can DATA parameters could be sent.

CURL:

$ curl https://api.iugu.com/v1/payment_token \
-d "account_id=xxxxxx" \
-d "method=credit_card" \
-d "data[number]=4111111111111111" \
-d "data[verification_value]=123" \
-d "data[first_name]=Joao" \
-d "data[last_name]=Silva" \
-d "data[month]=12" \
-d "data[year]=2013"


Reference: https://iugu.com/referencias/api#criar-um-token

My application with parameters and get ID as return:

try {
URL url = new URL(urlString);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
String userCredentials = "xxxxxxxx"; //TOKEN
String basicAuth = "Basic " + Base64.encodeToString((userCredentials).getBytes(), Base64.DEFAULT);
urlConnection.setRequestProperty ("Authorization", basicAuth);

JSONObject jsonObject = new JSONObject();
jsonObject.put("account_id", params[1]);
jsonObject.put("method", params[2]);
jsonObject.put("test", params[3]);
jsonObject.put("data[number]", params[4]);
jsonObject.put("data[verification_value]", params[5]);
jsonObject.put("data[first_name]", params[6]);
jsonObject.put("data[last_name]", params[7]);
jsonObject.put("data[month]", params[8]);
jsonObject.put("data[year]", params[9]);

OutputStreamWriter os= new OutputStreamWriter(urlConnection.getOutputStream());
os.write(jsonObject.toString());
os.flush();

int responseCode = urlConnection.getResponseCode();
Log.d(TAG, "IUGU responsecode " + responseCode);

StringBuilder responseStrBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String inputStr = null;
while ((inputStr = reader.readLine()) != null)
responseStrBuilder.append(inputStr);
InJsonObject = new JSONObject(responseStrBuilder.toString());
id = InJsonObject.get("id").toString();
Log.d(TAG, "IUGU id " + id);
os.close();

Answer

Just create data as another JSONObject, fill its fields the same way as you filling jsonObjects's fields calling put, then put it inside jsonObject, i.e. replace

jsonObject.put("data[number]", params[4]);
jsonObject.put("data[verification_value]", params[5]);
jsonObject.put("data[first_name]", params[6]);
jsonObject.put("data[last_name]", params[7]);
jsonObject.put("data[month]", params[8]);
jsonObject.put("data[year]", params[9]);

with

JSONObject data = new JSONObject();
data.put("number", params[4]);
data.put("verification_value", params[5]);
data.put("first_name", params[6]);
data.put("last_name", params[7]);
data.put("month", params[8]);
data.put("year", params[9]);
jsonObject.put("data", data);
Comments