surya surya - 10 days ago 6
Android Question

How to parse JSON GET method in android with user input parameters?

How to get data from JSON with user input parameters in android?

Answer

I believe the below code will give you the feasible solution for your problem,

Paste the below code in your Java file

private static JSONObject get(Context ctx, String sUrl) {
    HttpURLConnection connection = null;

    try {

        URL url = new URL(sUrl);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Authorization",
                "Basic " + encodedAuthentication);
        connection.setRequestProperty("Accept-Charset", "utf-8,*");
        Log.d("Get-Request", url.toString());
        try {
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
            bufferedReader.close();
            Log.d("Get-Response", stringBuilder.toString());
            return new JSONObject(stringBuilder.toString());
        } finally {
            connection.disconnect();
        }
    } catch (Exception e) {
        Log.e("ERROR", e.getMessage(), e);
        return null;
    }
}

private static String buildSanitizedRequest(String url,
                                            Map<String, String> mapOfStrings) {

    Uri.Builder uriBuilder = new Uri.Builder();
    uriBuilder.encodedPath(url);
    if (mapOfStrings != null) {
        for (Map.Entry<String, String> entry : mapOfStrings.entrySet()) {
            Log.d("buildSanitizedRequest", "key: " + entry.getKey()
                    + " value: " + entry.getValue());
            uriBuilder.appendQueryParameter(entry.getKey(),
                    entry.getValue());
        }
    }
    String uriString;
    try {
        uriString = uriBuilder.build().toString(); // May throw an
        // UnsupportedOperationException
    } catch (Exception e) {
        Log.e("Exception", "Exception" + e);
    }

    return uriBuilder.build().toString();

}

And your Json calling part should look like this

public static JSONObject exampleGetMethod(Context ctx, String sUrl,String yourName) throws JSONException, IOException {
    Map<String, String> request = new HashMap<String, String>();
    request.put("yourName", yourName);

    sUrl = sUrl + "yourApiName";
    return get(ctx, buildSanitizedRequest(sUrl, request));
}

In the above code - startDate, endDate,yourName and username are the input parameters. sUrl is the API url + API name Finally when you call exampleGetMethod(Context,String,String,String,String,String); you will get the JSON response of requested URL.

If you want to get the specific array value from the response you need to think of below logic

JSONArray a = response.getJSONArray("transactions");
JSONObject needyArray;
for (int i = 0; i < a.length(); i++) {
        if(a.getJSONObject(i).optString("name").equals("kumar")){
            needyArray = a.getJSONObject(i);
            break;
        }
}

Now the needyArray JSONObject variable have the data of particular person(kumar as the example)