JSON Parsing GET method, trying to display values in Spinner

how to display json data in spinner in android
android spinner example to load json using retrofit
how do i populate an android spinner using a json web service?
country, state city spinner in android using json
how to get data from mysql database to spinner in android
how to display selected value in spinner in android
spinner android
spinner simplified coding

I am trying to display values in Spinner by using JSON parsing, but it's not taking the centerID and giving me error as " centerID not found". And, nothing is displaying in Spinner View.

// This is my API file

BASE_URL : http://192.168.1.80/aoplnew/api/
API URL : BASE_URL/users/getdoctorslist/$center_id
METHOD : GET
PARAMETER TO PASS:
                center_id=4
INPUT:
        BASE_URL/users/getdoctorslist/4
OUTPUT:
          {
    "status": true,
    "result": [
        {
            "doctor_id": "1",
            "first_name": "Dr. Sagar",
            "last_name": "Patil"
        },
        {
            "doctor_id": "2",
            "first_name": "Dr. Ashwini",
            "last_name": "D"
        }
    ],
    "message": "Get Doctors list successfully!"
}

Errors:
1)
       {
           "status": false,
        "message": "Center id should not empty!"
        }
2)
       {
           "status": false,
        "message": "There is no Center data found!"
}

This is the config file where I am doing Connection

public class Config {

  //JSON URL
  public static final String DATA_URL = "http://192.168.1.80/aoplnew/api/users/getdoctorslist/{center_id}";

  //Tags used in the JSON String
  public static final String TAG_DOC_ID = "doctor_id";
  public static final String TAG_FIRST_NAME = "first_name";
  public static final String TAG_LAST_NAME = "last_name";

  //JSON array name
  public static final String JSON_ARRAY = "result";
}

This is my fragment where I am trying to fetch the values in Spinner using Volley library

private ArrayList<String> docList;

//JSON Array
private JSONArray result;
private Spinner doctorLists;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add_patient, container, false);

    doctorLists = (Spinner) view.findViewById(R.id.doctors_list);
    //Initializing the ArrayList
    docList = new ArrayList<String>();
    //This method will fetch the data from the URL
    getData();
    return view;  
} // closing onCreateView()

private void getData(){
  //Creating a string request
  StringRequest stringRequest = new StringRequest(Request.Method.GET,Config.DATA_URL,
new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
      JSONObject j = null;
      try {
        //Parsing the fetched Json String to JSON Object
        j = new JSONObject(response);

        //Storing the Array of JSON String to our JSON Array
        result = j.getJSONArray(Config.JSON_ARRAY);

        //Calling method getStudents to get the students from the JSON Array
        getDoctors(result);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
  },
  new Response.ErrorListener() {
  @Override
    public void onErrorResponse(VolleyError error) {

    }
  });

  //Creating a request queue
  RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
  //Adding request to the queue
  requestQueue.add(stringRequest);
}

private void getDoctors(JSONArray j){
  //Traversing through all the items in the json array
  for(int i=0;i<j.length();i++){
    try {
      //Getting json object
      JSONObject json = j.getJSONObject(i);

      //Adding the name of the student to array list
      docList.add(json.getString(Config.TAG_FIRST_NAME));
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  //Setting adapter to show the items in the spinner
  doctorLists.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, docList));
}

you are missing request method

use this your Api need to pass center_id as parameter when its called

StringRequest stringRequest = new StringRequest(Request.Method.GET,Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);

//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);

//Calling method getStudents to get the students from the JSON Array
getDoctors(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}){ @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            params.put("center_id", "4");
            return params;
        };

//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(stringRequest);
}

if still it is not work let me know

JSON Parsing GET method, trying to display values in Spinner, you are missing request method. use this your Api need to pass center_id as parameter when its called StringRequest stringRequest = new  Revisited: Full-fledged client-server example with C++ REST SDK 2.10 Posted on November 19, 2017 November 20, 2017 by Marius Bancila Four years ago I wrote a blog post that shown how to build a web server using the http_listener from the C++ REST SDK library as well as a client application that consumed the exposed resources.

StringRequest stringRequest = new StringRequest(Config.DATA_URL,...

You're not setting the center_id argument in your request

How to Populate Spinner in Android With JSON Data [Snippet , Get the code for populating Spinner drop-down menus in Android apps with JSON data values. These snippets will let you parse data from a URL into the Spinner. of the placeholder and the example attribute to show how the string using the The Android method “getQuantityString()” cuts through the  The following are code examples for showing how to use dnslib.DNSRecord.parse(). They are from open source Python projects. They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

I'm not good at English.

I recommend use GSON for convert json string to object: https://github.com/google/gson

and recommend use retrofit for connect to service: https://square.github.io/retrofit/

Android parse json into spinner, In this Android, I am going to show you how to parse a JSON string stored in a file from a remote server into a ListView widget, you read Load mysql data in listview. jsonArray=null; ArrayList<String> cList=new ArrayList<String>(); try call fragment method from activity · Android get phone number from  Trying to display a list property in XtraReport c#,asp.net,asp.net-mvc,json.net. You try to add all your 52 parameter and their values with one AddWithValue

Already you have information about your API.

API URL : BASE_URL/users/getdoctorslist/$center_id
METHOD : GET
PARAMETER TO PASS:
                center_id=4
INPUT:
        BASE_URL/users/getdoctorslist/4

Your API need a value $center_id but you didn't pass the value. You requested url is

"http://192.168.1.80/aoplnew/api/users/getdoctorslist/{center_id}"

Please send a value of center_id.

Try this

final String url = "http://192.168.1.80/aoplnew/api/users/getdoctorslist/4";

    // prepare the Request
    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response) { 
                    try {
                      //Parsing the fetched JSON String to JSON Object
                        j = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = j.getJSONArray(Config.JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getDoctors(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", response);
                }
            }
    );

    // add it to the RequestQueue   
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

//Adding request to the queue requestQueue.add(stringRequest);

Android Native JSON Ep.01 : Spinner, Then we parse this data and show it in a spinner.We are using org.JSON classes,​the Duration: 12:58 Posted: Jul 8, 2016 Python pyvirtualdisplay.Display () Examples. The following are code examples for showing how to use pyvirtualdisplay.Display(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Android Spinner Example to Load JSON using Volley, And in the spinner we will show the values from MySQL database. So in this tutorial we will retrieve data from our webserver using JSON String. I have my JSONObject j = null;. try {. //Parsing the fetched Json String to JSON Object //​Calling method getStudents to get the students from the JSON Array. I want to display the text on clicking on the input Tag: jquery , html , css Okay so I am trying display the text on focus in the input field all i've done so far is this.

Okhttp json, P. In Last Post We have seen Android JSON Parsing Using okHttp What ever content do you want to send to server, it will use below key method. In this Example, you will learn how to populate a spinner with data you how to parse JSON data from a URL and display it in a TextView. okhttp-json-mock. Re: Trying to display on a collectionview cell. Level 8 (8,785 points) Claude31 Mar 27, 2020 12:35 AM ( in response to ZoneX )

JSON Parsing Tutorial With Example In Android Studio [Step by Step], Learn JSON parsing in Android Studio following our step by step tutorial with two 1. get(String name): This method is used to get the value from JSONObject. In this example we parse the data from JSON and then display it in the UI.In this findViewById(R.id.salary); try { // get JSONObject from JSON file JSONObject obj  Android :: Display Array Of Strings In Spinner With Spinner.setAdapter Jun 9, 2010. I have an xml layout file which contains a few widgets including a Spinner I want to display a list of strings in the spinner, the list is generated at runtime as a result of a function so it can not be in arrays.xml. I tried doing: CODE:..

Comments
  • i have made the changes,but still nothing is displaying in Spinner
  • i have edited my answer,try it.You are not passing "center_id" parameter when api called.
  • Or you can also try " Config.DATA_URL+YOUR CENTER_ID" in StringRequest
  • @Override // here im getting error public Map<String, String> getHeaders() throws AuthFailureError { // at throws im getting one more error Map<String, String> params = new HashMap<String, String>(); params.put("Content-Type", "application/json"); params.put("center_id", "4"); return params; };
  • have you checked your api url in postman? if you checked in postman then in postman you are getting correct respons?
  • @Override // here im getting error public Map<String, String> getHeaders() throws AuthFailureError { // at throws im getting one more error Map<String, String> params = new HashMap<String, String>(); params.put("Content-Type", "application/json"); params.put("center_id", "4"); return params; };