0

我在我的 UI 线程中创建了一个名为 FetchTaskList 的类,它扩展了 AsyncTask。我正在使用服务器套接字并尝试从套接字的输入流中读取。调试时,doInBackground 会在 while 循环结束后停止,因此永远不会调用 onPostExecute。为什么会这样?这几天我一直在头疼……

任务列表活动.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 中没有得到任何东西,所以没有发现异常。

4

1 回答 1

0
while(scanner.hasNext())

应该:

while(scanner.hasNextLine())
于 2013-07-14T18:14:14.277 回答