问题
I am trying to fetch json data from server using volley library and store in array list then display data using spinner.
my code on php server side:
echo json_encode(array('result'=>$result));
My fetch data function:
private void getEventRespondTest (RequestQueue requestQueue) {
//Creating a string request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,Config.DATA_URL,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject respond) {
try {
eventArray = respond.getJSONArray("result");
Toast.makeText(Beacon_MainActivity.this,eventArray.toString(),Toast.LENGTH_LONG).show();
eventDetail = getEventDetail(eventArray);
} catch (JSONException e) {
e.printStackTrace();
}
// }
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Beacon_MainActivity.this, "Unable to fetch data: " +error.getMessage(),Toast.LENGTH_LONG).show();
}
}
);
//Adding request to the queue
requestQueue.add(jsonObjectRequest);
}
private ArrayList getEventDetail(JSONArray j) {
ArrayList event = new ArrayList();
//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 event to array list
event.add(json.getString(Config.EVENT_TITLE));
} catch (JSONException e) {
e.printStackTrace();
}
}
Toast.makeText(Beacon_MainActivity.this,event.toString(),Toast.LENGTH_LONG).show();
return event;
}
And on my OnCreate method I am trying to fetch the data and display result using spinner but when run, result return first time is blank "
eventDetail = new ArrayList<>();
eventArray = new JSONArray();
// //get event from server
getEventRespondTest(Volley.newRequestQueue(Beacon_MainActivity.this));
spinner.setAdapter(new ArrayAdapter<>(Beacon_MainActivity.this, android.R.layout.simple_spinner_dropdown_item, eventDetail));
Any help is much appreciate. Thanks.
回答1:
Your function private void getEventRespondTest is returning null as it is Void. What are you doing with eventDetail = getEventDetail(eventArray); getEventRespondTest function? Or tried to change POST to GET?
UPDATE CHECK THIS CODE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getEventRespondTest();
}
private void getEventRespondTest () {
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
StringRequest request = new StringRequest("http://vinhvumobile.com/phpconnect/geteventdetail.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// we got the response, now our job is to handle it
getDataFromJson(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//something happened, treat the error.
Log.e("Error", error.toString());
}
});
queue.add(request);
}
private ArrayList getDataFromJson(String json) {
ArrayList events=new ArrayList();
JSONArray array;
try {
JSONObject gallery=new JSONObject(json);
array=gallery.getJSONArray("result");
//Do sth with it
for(int i=0;i<array.length();i++){
JSONObject jsonOBJ = (JSONObject) array.get(i);
events.add(jsonOBJ.get("EventID"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return events;
}
回答2:
I think you response in method public void onResponse(JSONObject respond) respond is null because you use JsonObjectRequest to make request but your server(php) return a String it mean nothing JsonObject to return. Try use StringRequest instead of JsonObjectRequest . In best solution is change code your server for it return a `JsonObject' follow this (php file):
header('Content-Type: application/json');
echo json_encode(array('result'=>$result), 256); // 256 for UTF-8 data
来源:https://stackoverflow.com/questions/33991227/json-array-return-blank-when-run-oncreate-android