这个错误很常见,但尽管应用了最受欢迎的解决方案几天,我仍然陷入困境。我有几个扩展AsyncTask的类。
奇怪的是我的PostComment.java
会调用doInBackground但DownloadComments.java
不会,尽管它们一个接一个地处于同一个函数中。我调用执行的顺序无关紧要。
Bios.java 调用DownloadComments和PostComment
@Override
public void onClick(View v) {
switch(v.getId()) {
case(R.id.Post):
// Start DownloadComments
// Execute doInBackground NOT CALLED
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new DownloadComments().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "Clinton");
} else {
new DownloadComments().execute(candidate);
}
// Post Comment
EditText commentHandler = (EditText) findViewById(R.id.comment);
String commentString = commentHandler.getText().toString();
// Execute doInBackground IS CALLED
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new PostComment().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, commentString, MainActivity.username, Integer.toString(MainActivity.PosterID));
} else {
new PostComment().execute(commentString, MainActivity.username, Integer.toString(MainActivity.PosterID));
}
break;
default:
break;
}
}
PostComment.java(extends AsyncTask<String, Void, Void>
)
@Override
protected Void doInBackground(String... arg0) {
// Otherwise SocketException Connection reset by peer
try {
String comment = arg0[0];
String username = arg0[1];
String PosterName = arg0[2];
// EDIT LINK
String link = "http://particlecollider.net76.net/postComment.php";
URL url = new URL(link);
String data = URLEncoder.encode("comment", "UTF-8") + "="
+ URLEncoder.encode(comment, "UTF-8")
+ "&"
+ URLEncoder.encode("UN", "UTF-8") + "="
+ URLEncoder.encode(username, "UTF-8");
URLConnection conn = url.openConnection();
System.setProperty("http.keepAlive", "false");
// Kill keep-alive property
conn.setRequestProperty("connection", "close");
// Write
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Read
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
下载评论extends AsyncTask<String, Void, String>
此方法并未全部输入
@Override
protected String doInBackground(String... passedIn) {
try {
String candidate = passedIn[0];
String link = "http://particlecollider.net76.net/downloadComments.php?candidate="+candidate;
int CommentsInLinearList;
URL url = new URL(link);
URLConnection conn = url.openConnection();
System.setProperty("http.keepAlive", "false");
// Kill keep-alive property
conn.setRequestProperty("connection", "close");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
// Skip until it works
while (!(line = reader.readLine()).contains("COMMENTLINEARLIST")) {
// Finished
}
CommentsInLinearList = Integer.parseInt(reader.readLine());
while (CommentsInLinearList > 0) {
line = reader.readLine();
sb.append(line);
CommentsInLinearList--;
}
JSONObject jObject = new JSONObject(sb.toString());
JSONArray myJSONArray = jObject.getJSONArray("CommentLinearList");
if(myJSONArray != null) {
for(int i=0; i<myJSONArray.length(); i++) {
JSONObject targetComment = myJSONArray.getJSONObject(i);
Comment PushComment = new Comment(candidate);
PushComment._ID = Integer.parseInt(targetComment.getString("ID"));
PushComment._Candidate = candidate;
PushComment._PosterID = Integer.parseInt(targetComment.getString("PosterID"));
PushComment._ReplyToId = Integer.parseInt(targetComment.getString("ReplyToID"));
PushComment._Rating = Integer.parseInt(targetComment.getString("Rating"));
PushComment._Comment = targetComment.getString("Comment");
// PushComment into candidateCommentArrayList
candidateCommentArrayList.add(PushComment);
}
// getMenuInflater().inflate(R.menu., candidateCommentArrayList);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
调试时,它甚至不会为DownloadComments输入doInBackground
请帮忙。如果我可以添加更多详细信息,请告诉我