surya surya - 1 month ago 12
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?


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");
                "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) {
            Log.d("Get-Response", stringBuilder.toString());
            return new JSONObject(stringBuilder.toString());
        } finally {
    } 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();
    if (mapOfStrings != null) {
        for (Map.Entry<String, String> entry : mapOfStrings.entrySet()) {
            Log.d("buildSanitizedRequest", "key: " + entry.getKey()
                    + " value: " + entry.getValue());
    String uriString;
    try {
        uriString =; // May throw an
        // UnsupportedOperationException
    } catch (Exception e) {
        Log.e("Exception", "Exception" + e);



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++) {
            needyArray = a.getJSONObject(i);

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