I use volley to respond from the API, but the response consists of a pair of STATE_ID: STATE_NAME (ie value:value pair) , and I need both values ββon different lines. I need these values ββto fit in the spinner so that when the user selects State , I can also get its corresponding identifier.
// JSON response { "1": "West bengal", "3": "Himachal Pradesh", "4": "Maharashtra", "11": "Queensland" }
My code
public class MainActivity extends AppCompatActivity { private static final String STATE = "MY_API"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void login(View v){ loginRequest(); } private void loginRequest() { StringRequest stringRequest = new StringRequest(Request.Method.POST, STATE, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("country_id","2"); return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); requestQueue.add(stringRequest); } }
source share