我无法添加我的ArrayList [Android]

时间:2016-10-10 03:48:04

标签: java android arraylist

在我的课程中,我创建了一个名为variable的{​​{1}},并在taskArray中对此进行了初始化

onCreate

然后在 public class ActivityOne extends Activity { ArrayList<WorkTask> taskArray = null; @Override protected void onCreate(Bundle savedState) { taskArray = new ArrayList<WorkTask>(); final GetTasks getTasks = new GetTasks(); getTasks.execute(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { if ( getTasks.getStatus() == AsyncTask.Status.RUNNING ) { getTasks.cancel(true); Toast.makeText(ActivityOne.this, "Connection Timeout", Toast.LENGTH_SHORT).show(); } } }, 60000 ); } AsyncTask doInBackground我在branchesArray添加数据,但它没有添加。在它正常工作之前,但现在它没有。我的代码看起来像这样

 public final class GetTasks extends AsyncTask<Void, Void, Employee> {
   protected Employee doInBackground(Void... params) {

     try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(URL_STRING);
            HttpResponse response = httpclient.execute(httpget);
            str =  EntityUtils.toString(response.getEntity());
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        try{
            JSONObject jsonObject = new JSONObject(str);
            JSONObject data = jsonObject.getJSONObject("DATA");

            //Employee info
            empObj = new Employee();

            empObj.setId(data.getInt("id"));
            empObj.setName(data.getString("name").toString());

            JSONArray jArray = data.getJSONArray("worktask");

            for (int i = 0, count = jArray.length(); i < count; i++) {
                JSONObject jObj = jArray.getJSONObject(i);

                WorkTask workTask = new WorkTask();

                workTask.setId(jObj.getInt("id"));
                workTask.setTaskName(jObj.getInt("task_name"));

                taskArray.add(workTask);
            }

        }catch (JSONException e){
            e.printStackTrace();
            return null;
        }

        return empObj;
    }

这是我的模型WorkTask

public class WorkTask implements Serializable {
 private int id;
 private String taskName;

  public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

 public int getTaskName() {
    return taskName;
}

 public void setTaskName(int taskName) {
    this.taskName = taskName;
}

2 个答案:

答案 0 :(得分:0)

我认为您可以撰写有关代码的更多信息。

count发生的事件为零,表示jArray为空列表。

首先,你检查你的json数据worktask

答案 1 :(得分:0)

我在这里犯了一个愚蠢的错误,我的其他价值是从jObj获取应该来自empObj的数据,这就是为什么它总是转到catch。我现在已经修好并正常工作了。