我正在尝试为我的JsonArray创建一个异步任务,因为它正在跳帧和滞后。我创建了一个放入参数的asyncTask类,我在后台执行了调用我的Json请求,但是,我现在如何在我的postexecute方法中引用我的Json onresponse?
class MyTask extends AsyncTask<URL,String,JSONArray> {
@Override
protected JSONArray doInBackground(URL... params) {
JsonRequestMethod();
return null;
}
@Override
protected void onPostExecute(JSONArray response) {
}
}
}
private void JsonRequestMethod() {
mVolleySingleton = VolleySingleton.getInstance();
//intitalize Volley Singleton request key
mRequestQueue = mVolleySingleton.getRequestQueue();
//2 types of requests an Array request and an Object Request
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, URL_API, (String) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
listblogs.clear();
listblogs=parseJSONResponse(response);
mAdapterDashBoard.setBloglist(listblogs);
System.out.println("it worked!!!");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mRequestQueue.add(request);
}
private ArrayList<Blogs> parseJSONResponse(JSONArray response) {
if (!response.equals("")) {
ArrayList<Blogs> blogsArrayList = new ArrayList<>();
try {
StringBuilder data = new StringBuilder();
for (int i = 0; i < response.length(); i++) {
JSONObject currentQuestions = response.getJSONObject(i);
String text = currentQuestions.getString("text");
String points = currentQuestions.getString("points");
String ID=currentQuestions.getString("id");
String studentId = currentQuestions.getString("studentId");
String DateCreated=currentQuestions.getString("created");
long time=Long.parseLong(DateCreated.trim());
data.append(text + "\n" + points + "\n");
System.out.println(data);
Blogs blogs = new Blogs();
blogs.setId(ID);
blogs.setMstudentId(studentId);
blogs.setMtext(text);
blogs.setPoints(points);
//The dateCreated was off by 1 hour so 3600000 ms where added=1hour, (UPDATE)
blogs.setDateCreated(getTimeAgo(time));
System.out.println(time+"time");
listblogs.add(blogs);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return listblogs;
}
答案 0 :(得分:3)
你的截击请求已经异步,这就是为什么它有onResponse()
和onErrorResponse()
等侦听器接口。你根本不需要它AsyncTask
。从UI线程调用您的截击请求,并使用AsyncTask
来解析JSON。例如:
onResponse()
中,创建AsyncTask以处理解析。doInBackground()
方法中解析JSON,“返回”解析的结果。onPostExecute()
会收到解析“结果”,您可以在那里做你想做的事。为了更进一步,您可以为您的AsyncTask实现一个侦听器接口,例如onJSONParsed()
,然后您可以在UI线程中设置逻辑(您调用您的截击请求),以处理100 %完成回复。例如:
class MyTask extends AsyncTask<URL,String,ArrayList<Blogs>> {
// Listener member variable.
private OnJSONParsedListener mOnJSONParsedListener;
@Override
protected ArrayList<Blog> doInBackground(URL... params) {
// Parse JSON.
ArrayList<Blogs> blogsList = parseJSON(response);
// Pass the blogs list to the onPostExecute method.
return blogsList;
}
@Override
protected void onPostExecute(ArrayList<Blogs> blogsList) {
// Invoke listener, if present.
if (mOnJSONParsedListener != null)
mOnJSONParsedListener.onJSONParsed(blogsList);
}
// Listener interface.
public interface OnJSONParsedListener {
void onJSONParsed(ArrayList<Blogs> blogsList);
}
// Setter for listener interface member variable.
public void setOnJSONParsedListener(OnJSONParsedListener listener) {
mOnJSONParsedListener = listener;
}
}
然后在你的UI线程中。
// Call your volley request.
mVolleySingleton = VolleySingleton.getInstance();
//intitalize Volley Singleton request key
mRequestQueue = mVolleySingleton.getRequestQueue();
//2 types of requests an Array request and an Object Request
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, URL_API, (String) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Create AsyncTask.
MyTask parseJSONTask = new MyTask();
// Set listener interface.
parseJSONTask.setOnJSONParsedListener(new MyTask.OnJSONParsedListener() {
@Override
public void onJSONParsed(ArrayList<Blogs> blogsList) {
// Do stuff with your blogs list.
mAdapterDashBoard.setBloglist(blogsList);
// It worked!
System.out.println("it worked!!!");
}
}
// Execute.
parseJSONTask.execute(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mRequestQueue.add(request);
希望这有帮助。