How to convert the curl request into java (Android)

1

I hope to convert the below example in CURL to Json in Java / Android.

The IUGU API supports JSON or XML. I tried several examples on the internet, but without success.

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: link

Part of my application, with parameters and getting the return ID:

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();
    
asked by anonymous 24.08.2016 / 17:41

1 answer

0

Solution is to create another JSON, "date". And then insert the "date" into jsonObject.

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 jsonObject = new JSONObject();
jsonObject.put("account_id", params[1]);
jsonObject.put("method", params[2]);
jsonObject.put("test", params[3]);
jsonObject.put("data", data);
    
28.08.2016 / 07:01