开发者

Why do I keep getting 'Bad Request' when I try to post a task in Java using the Google Task API?

I'm building an app coded in Java/Android and using the Google Task API but I have hit a major problem.

I try to enter a Due or Completed date the Task API returns a 'Bad Request' error. I have however managed to post tasks successfully when I don't attempt to set a Due or Completed date.

I have spent hours researching this issue the closest I have found to a solution is the following thread:

http://groups.google.com/group/google-tasks-api/browse_thread/thread/1753df5bbc8e3cb8/ab66b5e675f06e6a?lnk=gst&q=problem+changing#ab66b5e675f06e6a

I have attempted to set the task's status along with a date as mentioned in the thread but that doesn't seem to work for me.

I presuming I have formatted the DateTime wrong, although I'm just using a DateTime as specified in the Google docs (com.google.api.client.util.DateTime).

This is an example of a typical DateTime value that I'm attempting to use:

2011-10-03T22:28:626

I have tried the following code...

DateTime date = new DateTime(new Date());
Task task = new Task();
task.setTitle("Hello World");
task.setDue(date);
task.setStatus("needsAction");

and ...

DateTime date = new DateTime(new Date());   
Task task = new Task();
task.setTitle("Hello World");
task.setCompleted(date);
task.setStatus("completed");

I would be very grateful if anyone can help me.

Thank you.


Thank you for everyone who viewed.

After a little more digging I found the answer so here it is...

The Google Task API currently only accepts a DATETIME.

I开发者_StackOverflow was providing a DateTime but the format was incorrect, I had to add a Time Zone Shift Integer. e.g. com.google.api.client.util.DateTime.DateTime(long value, Integer tzShift).

I simply did the below instead and it worked.

DateTime date = new DateTime(System.currentTimeMillis(), 0);
Task task = new Task();
task.setTitle("Hello World");
task.setDue(date);
task.setStatus("needsAction");


What happens if you try providing the date as a String?

According to the developer docs, this should be valid. For instance, this is one of the examples provided:

Task task = new Task();
task.title = "New Task";
task.notes = "Please complete me";
task.due = "2010-10-15T12:00:00.000Z";

Task result = service.tasks.insert("@default", task).execute();
System.out.println(result.title);

[From: http://code.google.com/apis/tasks/v1/using.html#creating_task ]

If this works, it's possible that there's a bug in the client library when converting your DateTime object into a JSON string.


Use:

DateTime date = new DateTime(System.currentTimeMillis(),0);
task.setDue(date);

or
new DateTime(new Date(value), TimeZone.getDefault());

Don't use:

DateTime date = new DateTime(System.currentTimeMillis());


There are one more possibly. I use a thread to run the update.execute() coding and case the same result. (Bad request / 400 / Invaild input)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜