[英]why does not the Tag show my the array element?
it gives me a bunch of errors in the logcat.它在 logcat 中给了我一堆错误。 but if I delete Log.d("intheclass", questionsArray.get(0));
但是如果我删除Log.d("intheclass", questionsArray.get(0));
it shows no errors and app launches.它显示没有错误并且应用程序启动。 with that code, app doesn't launch.使用该代码,应用程序无法启动。 I have tried to LOG the response itself, it works.我试图记录响应本身,它有效。 Absolutely have no idea why this array doesn't work.完全不知道为什么这个数组不起作用。
package com.example.trivia.data;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.example.trivia.controller.AppControl;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
public class QuestionBank {
private String que1;
String url = "https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements.json";
ArrayList<String> questionsArray= new ArrayList<String>();
public List<String> getQuestions(){
JsonArrayRequest bank = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i=0; i<response.length(); i++){
try {
que1 = (String) response.getJSONArray(i).get(0);
Log.d("queva", String.valueOf(i));
questionsArray.add(que1);
} catch (JSONException e) {
e.printStackTrace();
}
}
//if(callback != null) callback.processFinish(questionsArray);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppControl.getInstance().addToRequestQueue(bank);
Log.d("intheclass", questionsArray.get(0));
return questionsArray;
}
}
it's empty by the time you try to access.当您尝试访问时,它是空的。 When you call getQuestions() it's going to another worker thread to add the values in the questionsArray but your log is on main thread and is in sequence call so it won't work and will give crash.当您调用getQuestions()时,它会转到另一个工作线程来添加questionsArray中的值,但您的日志在主线程上并且是按顺序调用的,因此它不会工作并且会崩溃。
Possible solution.可能的解决方案。
public void getQuestions(){
JsonArrayRequest bank = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i=0; i<response.length(); i++){
try {
que1 = (String) response.getJSONArray(i).get(0);
Log.d("queva", String.valueOf(i));
questionsArray.add(que1);
doSomething(); //here calling
} catch (JSONException e) {
e.printStackTrace();
}
}
//if(callback != null) callback.processFinish(questionsArray);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
and call this from onRepsonse() after successful api call.并在成功 api 调用后从 onRepsonse() 调用它。
public void doSomething(){
AppControl.getInstance().addToRequestQueue(bank);
Log.d("intheclass", questionsArray.get(0));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.