我无法从AsyncTask获得结果?

时间:2014-07-17 10:23:13

标签: java android mysql multithreading android-asynctask

此代码是否为真。我使用AsyncTask获取结果但是看不到任何结果而我正在使用WampServer。我认为我的错误逻辑在我的getter/setter

public class NotesActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                WebPageTask task = new WebPageTask();
                task.execute(new String[] { "http://192.168.5.170:8081/note-server/" });
    }

    private class WebPageTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
            ArrayList<StructTask> nettasks = new ArrayList<StructTask>();
            JSONObject object = null;
            JSONArray tasks = null;
            StructTask task = new StructTask();
            try {
             tasks = new JSONArray(result);
                for (int i = 0; i < tasks.length(); i++) {
                    object = tasks.getJSONObject(i);

                    task.setid(object.getLong("task_id"));
                    task.settitle(object.getString("task_title"));
                    task.setdesc(object.getString("task_desc"));
                    task.settone(object.getBoolean("task_done"));
                    nettasks.add(task);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.i("LOG","dd");
            for (StructTask taskesss : nettasks) {
                Log.i("LOG", "Taskes: " + taskesss.getid()+"|"+taskesss.gettitle()+"|"+taskesss.getdesc() + "-" + taskesss.getdone());
            }

        }
    }
}

这是Getter/Setter中的StructTask.java。我认为我的class getter/setter

中存在逻辑错误
public class StructTask {
    private long id;
    private String title;
    private String desc;
    private boolean done;

    public long getid() {
        return this.id;
    }

    void setid(long id) {
        this.id = id;
    }

    public String gettitle() {
        return this.title;
    }

    void settitle(String title) {
        this.title = title;
    }

    public String getdesc() {
        return this.desc;
    }

    void setdesc(String desc) {
        this.desc = desc;
    }

    public boolean getdone() {
        return this.done;
    }

    void setdone(boolean done) {
        this.done = done;
    }
}

Bellow代码不会返回任何结果:

    for (StructTask taskesss : nettasks) {
        Log.i("LOG", "Taskes: " + taskesss.getid()+"|"+taskesss.gettitle()+"|"+taskesss.getdesc() + "-" + taskesss.getdone());
    }

这是我的sql页面: THIS ONE AND THIS ONE

1 个答案:

答案 0 :(得分:0)

您的代码是真的。但是:

1-在您的getter/setter班级中:

private boolean done;
public boolean getdone() {
    return this.done;
}

void setdone(boolean done) {
    this.done = done;
}

更改为:

private int done;
public int getdone() {
    return this.done;
}

void setdone(int done) {
    this.done = done;
}

2-并改为:

.....你的代码......

@Override
protected void onPostExecute(String result) {
    ArrayList<StructTask> nettasks = new ArrayList<StructTask>();
    try {
        JSONArray tasks = new JSONArray(result);
        for (int i = 0; i < tasks.length(); i++) {
            JSONObject object = tasks.getJSONObject(i);
            StructTask task = new StructTask();
            task.setid(object.getLong("task_id"));
            task.settitle(object.getString("task_title"));
            task.setdesc(object.getString("task_desc"));
            task.setdone(object.getInt("task_done"));
            nettasks.add(task);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

......你的代码......

3-通过Thethering & Wi-Fi hotspot连接到您的电脑和Android,此选项位于您的setting手机中,然后通过Wi-Fi连接到您的手机。现在你可以看到你最喜欢的结果。

好看