kitkat kitkat - 1 month ago 12
Android Question

How to call AsynTask when my date is changed.?

I have a Date Picker , I want to load my asyncTask when I choose date from Date Picker,
my Code is here

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");


problem is there How to set date dynamically in my JSON URL to get data when I select date from date Picker. Please help me.

My LogCat details are here

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

Answer

How to set date dynamically in my JSON URL to get data when I select date from date Picker

Call ParentInputData().execute() inside onDateSet method of OnDateSetListener and pass updated url as parameter to execute method like:

   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
    }

Now use params[0] in doInBackground to get url :

    @Override
    protected JSONObject doInBackground(String... params) {

    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJSONFromUrl(params[0]);
    ....
  }