I am trying to retrieve JSON data from a server and then display that in a spinner, But the data is not showing up in the spinner, I am using volley to call to the server. How can i get the json data to show up in the spinner?
This is for testing pruposes, The spinner will retrieve catergories for upload location on my server then you select a category and then it uploads a file to that category also it will be using the selected categroy to link to uri.builder or something similiar to generate the URL for the location.
MainActivity.java
package com.smartpractice.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Spinner spinner;
String URL="https://www.smarhdhdh.co.za/app-categories.asp";
ArrayList<String> categories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categories=new ArrayList<>();
spinner=findViewById(R.id.country_Name);
loadSpinnerData(URL);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String country= spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
Toast.makeText(getApplicationContext(),country,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
}
private void loadSpinnerData(String url) {
RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject=new JSONObject(response);
if(jsonObject.getInt("success")==1){
JSONArray jsonArray=jsonObject.getJSONArray("categories");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject1=jsonArray.getJSONObject(i);
String cat1=jsonObject1.getString("cat1");
categories.add(cat1);
JSONObject jsonObject2=jsonArray.getJSONObject(i);
String cat2=jsonObject2.getString("cat2");
categories.add(cat2);
JSONObject jsonObject3=jsonArray.getJSONObject(i);
String cat3=jsonObject3.getString("cat3");
categories.add(cat3);
}
}
spinner.setAdapter(new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, categories));
}catch (JSONException e){e.printStackTrace();}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
requestQueue.add(stringRequest);
}
Json data
ClientID = 5767
Username = [email protected]
Pwd = Smarthdhdhd@123
{"categories": { "cat1":"Ford", "cat2":"BMW", "cat3":"Fiat" }}
I assume the 'categories' field is not a JsonArray, it is a JsonObject instead. You should change your parsing code into
JSONObject categories = jsonObject.getJSONObject("categories");
String cat1 = categories.getString("cat1");
String cat2 = categories.getString("cat2");
String cat3 = categories.getString("cat3");`