0

I have a JSON object that looks like this (will be returned from flask API to android Studio),

[
  {
    "Drug_Name": "Lepirudin"
  }, 
  {
    "Drug_Name": "Cetuximab"
  }, 
  {
    "Drug_Name": "Geptanolimab"
  }, 
  {
    "Drug_Name": "Narsoplimab"
  }, 
  {
    "Drug_Name": "Ieramilimab"
  }, 
  {
    "Drug_Name": "Vibostolimab"
  }, 
  {
    "Drug_Name": "Volagidemab"
  }, 
  {
    "Drug_Name": "Quavonlimab"
  }, 
  {
    "Drug_Name": "AK119"
  }, 
  {
    "Drug_Name": "Allocetra"
  }
]

This is how am getting this data in Flask app

            myCursor=myConnection.cursor()
            #query
            SQLcommand="SELECT Drug_Name FROM Drug_Description"
            
            #executing above query
            myCursor.execute(SQLcommand)  
                 
            
            row_headers=[x[0] for x in myCursor.description] #this will extract row headers
            rv = myCursor.fetchall()
            json_data=[]
            for result in rv:
                json_data.append(dict(zip(row_headers,result)))
            return jsonify (json_data)

This is how I am getting a json object in android Studio

String url = "https://********.herokuapp.com/";                
  RequestQueue queue = Volley.newRequestQueue(getContext());                 
  JsonObjectRequest jsonObjectRequest = new JsonObjectRequest            
  (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                
                                                @Override
                                                // When It works
                                                public void onResponse(JSONObject response) {
                                                    try {
    //get the json object and iterate it here
                                                }, new Response.ErrorListener() {
                                                @Override
                                                // If it doesnt work what to do (Errors)
                                                public void onErrorResponse(VolleyError error) {
                                                Toast.makeText(getContext(),"Try 
                                                Again",Toast.LENGTH_LONG).show();
        }
                                    });
                            queue.add(jsonObjectRequest);

How can I iterate over these values and save each drug name in a string array in android studio (java).

1 Answer 1

1
    private void getdata(){   
    String url = "https://********.herokuapp.com/";                
    RequestQueue queue = Volley.newRequestQueue(getContext());   
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {    
        @Override    
        public void onResponse(JSONArray itemArray) {  
         List<String> allDrugs = new ArrayList<String>();
         for (int i=0; i<itemArray.length(); i++) {
         JSONObject data = itemArray.getJSONObject(i);
         String name = data.getString("Drug_Name");
         allDrugs.add(name);
        }
        }    
    }, new Response.ErrorListener() {    
        @Override    
        public void onErrorResponse(VolleyError error) {    
            Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();    
        }    
    });  
    requestQueue.add(jsonArrayRequest);  
} 

Slight modifications might be required but more or less you can use this function.

Sign up to request clarification or add additional context in comments.

3 Comments

Am getting this error W/System.err: org.json.JSONException: Not a primitive array: class org.json.JSONArray W/System.err: at org.json.JSONArray.<init>(JSONArray.java:118) At This Code: JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { List<String> allDrugs = new ArrayList<String>(); JSONArray itemArray= null; try { itemArray = new JSONArray(response);
I have updated my answer please check now. It must work.
Ok, Ill check this in a while and let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.