我尝试使用JsonArrayRequest检索json数据,然后将其添加到列表中。这是我的代码
public class QuestionDetailFragment extends Fragment {
RecyclerView cRecyclerView;
private static final String url = "http://10.0.2.2:80/forumtest/readquestion.php?format=json";
private List<String> userList = new ArrayList<>();
RequestQueue requestQueue;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.question_details_layout, container, false);
readQuestionDetails();
Log.d("check", "data:" + userList.get(0));
return view;
}
private void readQuestionDetails() {
Log.d("Volley", "Called222!");
requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i=0;i<response.length();i++) {
JSONObject jsonObject = response.getJSONObject(i);
userList.add(jsonObject.getString("user"));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("tag",e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.getMessage());
}
});
requestQueue.add(jsonArrayRequest);
}
}
我使用了 Log.d(&#34;用户`,&#34;数据:&#34;,user.get(0)); < / strong>只是为了确保数据已添加到列表中。这是我运行代码后的logcat
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: FATAL EXCEPTION: main
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: Process: com.rekoj.softwarica, PID: 2767
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at java.util.ArrayList.get(ArrayList.java:308)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at com.rekoj.softwarica.Forum.QuestionDetailFragment.onCreateView(QuestionDetailFragment.java:50)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-08 20:45:25.750 2767-2767/com.rekoj.softwarica E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-08 20:50:25.802 2767-2767/? I/Process: Sending signal. PID: 2767 SIG: 9
答案 0 :(得分:1)
堆栈跟踪指出列表userList
为空,这就是您获得IndexOutOfBoundsException
的原因,readQuestionDetails
中的获取逻辑是异步执行的。
答案 1 :(得分:0)
把这一行
Log.d("check", "data:" + userList.get(0));
以下
userList.add(jsonObject.getString("user"));
Log.d("check", "data:" + userList.get(0));
这是因为下面这两个方法调用在不同的线程中运行
readQuestionDetails();
Log.d("check", "data:" + userList.get(0));