TransWikia.com

How do i call API with multiple parameters in android studio

Stack Overflow Asked by Shane on February 23, 2021

I have created this app that pulls data from an API and shows it in a list. the problem I am having is that I can’t pull the JSON data from an API with a nested JSON array.

In this image it is simple since all the info is in one array / table.

https://imgur.com/a/v9gsbop

but in this image, it is more difficult for me. for example, how do i call the paragraph value: line in body?

https://imgur.com/Qj5CRn8

This is the code that i am currently using to pull data from API.

private  void parseJSON () {
    String url = "https://blah,com";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,url,null,
            new Response.Listener<JSONObject>() {


        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONArray jsonArray =response.getJSONArray("items");

                for (int i = 0; i< jsonArray.length(); i++){
                    JSONObject article = jsonArray.getJSONObject(i);

                    String authorName = article.getString("article_author");
                    String imageUrl = article.getString("src");
                    String published = article.getString("first_published_at");
                    String description = article.getString("value");
                    String headline = article.getString("title");

3 Answers

Try this way to work with nested json array, enter image description here

Try this to get the result,
enter image description here

Correct answer by G Ganesh on February 23, 2021

Solution: If you want to pass multiple parameters to your request then,

Map<String, String> params = new HashMap();
params.put("first_param", 1);
params.put("second_param", 2);

JSONObject parameters = new JSONObject(params);

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() { ... }

Solution: If you want to pull the items array and get its subsequent object contents, try using an iterator instead of a for loop,

JSONArray jsonArray = (JSONArray) apiResult[0].get("body");
Iterator jsonArrayIterator = jsonArray.iterator();
while(jsonArrayIterator.hasNext()) {
    JSONObject jsonObject = (JSONObject) jsonArrayIterator.next();
    JSONObject jsonValue = (JSONObject) jsonObject.get("value");
    JSONObject jsonOriginal = (JSONObject) jsonValue.get("original");
    JSONObject jsonWidth = (JSONObject) jsonOriginal.get("width");
}

Answered by Shujath on February 23, 2021

Your question is not quiet clear, but what from what I understand I do think you need this solutions:

Solution: Sending Parameters along with the Request

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
    (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
    textView.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
    // TODO: Handle error

}
}){
   @Override
   protected Map<String, String> getParams() throws AuthFailureError {
       Map<String, String> params = new HashMap<>();
       params.put("parameter1 name","parameter1 Value");
       params.put("parameter2 name","parameter2 Value");
       return params;
};

As in the above example I have shown a get request; So suppose the URL is: "https://www.youtube.com/channel/UCXEIUll8VvOqRN-OSZ5_aOg" and parameter is "view_as" and parameter value is "subscriber" then params.put("view_as","subscriber");

Answered by Sayok Majumder on February 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP