How to convert Curl request to java (Android)

Asked

Viewed 281 times

1

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

The IUGU API accepts JSON or XML. I tried several examples on the internet, but to no avail.

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/references/api#create-a-token

Part of my application, with parameters and picking up 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();

1 answer

0

The solution is to create another JSON, "data". 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.