我在我的UI线程中创建了一个名为FetchTaskList的类,它扩展了AsyncTask。 我正在使用服务器套接字并尝试从套接字的输入流中读取。 调试时,doInBackground在while循环结束后停止,因此永远不会调用onPostExecute。为什么会这样?几天来,我一直在打破这个......
TaskListActivity.java
private class FetchTaskList extends AsyncTask<Void, Void, JSONArray> {
@Override
protected JSONArray doInBackground(Void... params)
{
JSONArray jArray = new JSONArray();
try
{
socket = new Socket(DEFAULT_HOST, DEFAULT_PORT);
Scanner scanner = new Scanner(socket.getInputStream());
PrintStream out = new PrintStream(socket.getOutputStream());
while(scanner.hasNext())
{
JSONObject json = new JSONObject(scanner.nextLine());
jArray.put(json);
}
return jArray;
}
catch (UnknownHostException e)
{
e.printStackTrace();
Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
} catch (IOException e)
{
e.printStackTrace();
Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}
catch (JSONException e)
{
e.printStackTrace();
Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}
return jArray;
}
@Override
protected void onPostExecute(JSONArray list)
{
for (int i = 0; i < list.length(); i++)
{
try
{
JSONObject obj = new JSONObject();
obj = list.getJSONObject(i);
taskList.add(new Task(obj.get("author").toString(),obj.get("description").toString(),(Date)obj.get("due"),(Task.Priority)obj.get("priority"),(Task.Status)obj.get("status")));
}
catch(Exception e)
{
Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}
}
ListView view = (ListView) findViewById(R.id.taskListView);
TaskListAdapter theAdapter = new TaskListAdapter(taskList,getApplicationContext());
view.setAdapter(theAdapter);
view.invalidate();
}
}
编辑:我在LogCat中没有得到任何内容,因此没有异常被捕获。
答案 0 :(得分:0)
while(scanner.hasNext())
应该是:
while(scanner.hasNextLine())