问题描述
我试图用凌空库,存放在数组列表,然后使用微调显示数据来从服务器的JSON数据。
I am trying to fetch json data from server using volley library and store in array list then display data using spinner.
在PHP服务器端我的code:
my code on php server side:
echo json_encode(array('result'=>$result));
我取数据功能:
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;
}
和我的的OnCreate
方法,我尝试使用微调来获取数据并显示结果,但在运行时,结果返回第一次是空的
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));
任何帮助很多AP preciate。谢谢你。
Any help is much appreciate. Thanks.
推荐答案
您函数私人无效getEventRespondTest
被返回null,因为它是虚空
。你在做什么与eventDetail = getEventDetail(eventArray);
getEventRespondTest
功能?或试图改变POST走?
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?
更新选中此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;
}
这篇关于JSON数组返回空白(QUOT;&QUOT;)时运行的OnCreate(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!