如何在我的日期发生变化时调用AsynTask。

时间:2016-01-20 09:38:14

标签: android android-asynctask

我有一个日期选择器,当我从Date Picker选择日期时,我想加载我的asyncTask, 我的代码在这里

datepicker.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);
        }
    });
     new ParentInputData().execute();


}

public void onStart(final Node node) {
    Looper.prepare();
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        // set date picker as current date
        return new DatePickerDialog(getParent().getParent().getParent(),
                datePickerListener, year, month, day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // set selected date into textview
        currDate.setText(new StringBuilder().append(month + 1).append("/")
                .append(day).append("/").append(year).append(" "));

        date = new StringBuilder().append(month + 1).append("/")
                .append(day).append("/").append(year).append(" ");

        currentDate = currDate.getText().toString();
         getParentInput();



    }

};
class ParentInputData extends AsyncTask<String, String, JSONObject> {


    String loginUrl = ("http://54.152.108.131/iphone111/getParentInput?child_id="+id+"&date="+date);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected JSONObject doInBackground(String... params) {
    System.out.println(loginUrl);

     JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(loginUrl);

        String nihal = json.toString();

        Log.d("JSIN RESPONSE :", nihal);

        return json;



    }

    @Override
    protected void onPostExecute(JSONObject json) {
        try {

            json_array = json.getJSONArray("CenterInfoData");
            for (int i = 0; i < json_array.length(); i++) {
                JSONObject c = json_array.getJSONObject(i);
                c = c.getJSONObject("ParentInput");

                String wake_up_time = c.getString("wake_up_time");
                String sleep_quality = c.getString("sleep_quality");
                String person_picking_child = c.getString("person_picking_child");
                String pick_up_time = c.getString("pick_up_time");
                String last_diaper_bm = c.getString("last_diaper_bm");
                String last_bottle_feeding = c.getString("last_bottle_feeding");
                String last_medication_name = c.getString("last_medication_name");
                String additional_notes = c.getString("additional_notes");

问题是当我从日期选择器中选择日期时,如何在我的JSON URL中动态设置日期以获取数据。请帮帮我。

我的LogCat详细信息在这里

01-20 05:08:34.547: E/AndroidRuntime(11352): FATAL EXCEPTION: AsyncTask #1
01-20 05:08:34.547: E/AndroidRuntime(11352): Process: com.MyKidzDay, PID: 11352
01-20 05:08:34.547: E/AndroidRuntime(11352): java.lang.RuntimeException: An error occured while executing doInBackground()
01-20 05:08:34.547: E/AndroidRuntime(11352):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at java.lang.Thread.run(Thread.java:841)
01-20 05:08:34.547: E/AndroidRuntime(11352): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 75: http://54.152.108.131/iphone111/getParentInput?child_id=6272&date=1/20/2016 
01-20 05:08:34.547: E/AndroidRuntime(11352):    at java.net.URI.create(URI.java:727)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at com.MykidzDayUpdate.JSONParser.getJSONFromUrl(JSONParser.java:36)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at com.KidsTabs.KidSummary$ParentInputData.doInBackground(KidSummary.java:1674)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at com.KidsTabs.KidSummary$ParentInputData.doInBackground(KidSummary.java:1)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-20 05:08:34.547: E/AndroidRuntime(11352):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-20 05:08:34.547: E/AndroidRuntime(11352):    ... 4 more

2 个答案:

答案 0 :(得分:1)

  

如何在我的JSON URL中动态设置日期以在我选择时获取数据   日期选择器的日期

ParentInputData().execute()的{​​{1}}方法中调用onDateSet,并将更新的网址作为参数传递给OnDateSetListener方法,如:

execute

现在使用 public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { ....your code here date = new StringBuilder().append(month + 1).append("/") .append(day).append("/").append(year).append(" "); String loginUrl="...?child_id="+id+"&date="+URLEncoder.encode(date.toString()); ParentInputData().execute(loginUrl); ....your code here } 中的params[0]获取网址:

doInBackground

答案 1 :(得分:0)

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // set selected date into textview
        currDate.setText(new StringBuilder().append(month + 1).append("/")
                .append(day).append("/").append(year).append(" "));

        date = new StringBuilder().append(month + 1).append("/")
                .append(day).append("/").append(year).append(" ");

        currentDate = currDate.getText().toString();
         getParentInput();

        new ParentInputData().execute(currentDate);
    }
};