Java 异步任务';s doInBackground()不会对某些任务执行(尽管有新的实例化)
这个错误很常见,但尽管应用了最流行的解决方案好几天,我还是被卡住了。我有几个类扩展了AsyncTask 奇怪的是,myJava 异步任务';s doInBackground()不会对某些任务执行(尽管有新的实例化),java,android,android-asynctask,Java,Android,Android Asynctask,这个错误很常见,但尽管应用了最流行的解决方案好几天,我还是被卡住了。我有几个类扩展了AsyncTask 奇怪的是,myPostComment。java确实调用doInBackground,但下载注释。java不,尽管它们一个接一个地在同一个函数中。我调用他们execute的顺序并不重要 java调用DownloadComments和PostComments @Override public void onClick(View v) { switch(v.getId()) {
PostComment。java
确实调用doInBackground,但下载注释。java
不,尽管它们一个接一个地在同一个函数中。我调用他们execute的顺序并不重要
java调用DownloadComments和PostComments
@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(扩展异步任务
)
DownloadComments扩展异步任务
根本不输入此方法
@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;
}
@覆盖
受保护的字符串doInBackground(字符串…passedIn){
试一试{
字符串候选者=passedIn[0];
字符串链接=”http://particlecollider.net76.net/downloadComments.php?candidate=“+候选人;
int注释线性列表;
URL=新的URL(链接);
URLConnection conn=url.openConnection();
set属性(“http.keepAlive”、“false”);
//杀生财产
conn.setRequestProperty(“连接”、“关闭”);
BufferedReader=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
StringBuilder sb=新的StringBuilder();
字符串行=null;
//读取服务器响应
//跳过直到它工作
而(!(line=reader.readLine()).contains(“COMMENTLINEARLIST”)){
//完成
}
CommentsInLinearList=Integer.parseInt(reader.readLine());
而(CommentsInLinearList>0){
line=reader.readLine();
某人附加(行);
评论线性主义者--;
}
JSONObject jObject=新的JSONObject(sb.toString());
JSONArray myJSONArray=jObject.getJSONArray(“CommentLinearList”);
if(myJSONArray!=null){
对于(int i=0;iIt没有输入PostComments的doInBackground,但它是否输入PostComments?是的,对于PostComments和DownloadComments,构造函数都会被调用。感谢您的响应。您是否将断点放在switch语句上?我没有,我很好奇它为什么会出现。我假设调试器没有通过因为mStatus总是挂起或运行,所以我一直在测试我能做的一切。现在我在评论PostComment的doInBackground
,我用DownloadComment的doInBackground
替换它,并在我调用DownloadComments的任何地方调用PostComment。奇怪的是,它进入了doInBackground wh恩,我这么做了,所以问题不在于方法的内容。我想知道为什么它区分DownloadComments.java而不是PostComment.java?
@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;
}