Android:使用 HTTPClient 将日期对象发布到 PHP

发布于 2024-11-29 03:28:40 字数 638 浏览 1 评论 0原文

我正在尝试使用 HTTPClient 将 Date 对象 Date date = new Date() 发布到远程 PHP 脚本,但似乎 NameValuePair 不接受除一个字符串?如果您能指导我如何使用 HTTPClient 发布 Date 对象,我将不胜感激

,这是我的代码

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
Date date = new Date();
postParameters.add(new BasicNameValuePair("stringObj", "Test")); //No error
postParameters.add(new BasicNameValuePair("dateTime", date));  //Error here

try{

    String response = CustomHttpClient.executeHttpPost("http://remotewebsite/test.php", postParameters);

catch{
// ...
}

I am trying to post a Date object Date date = new Date() to a remote PHP script using HTTPClient but it seems like NameValuePair does not accept any other Java objects other than a String? Would appreciate if you can guide me how to post a Date object using HTTPClient

Here's my code

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
Date date = new Date();
postParameters.add(new BasicNameValuePair("stringObj", "Test")); //No error
postParameters.add(new BasicNameValuePair("dateTime", date));  //Error here

try{

    String response = CustomHttpClient.executeHttpPost("http://remotewebsite/test.php", postParameters);

catch{
// ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2024-12-06 03:28:40

将其转换为字符串。确保 date 不为空/null(我在代码中没有看到您将 date 对象与 Date 中的某些方法一起使用代码 > 类)

例如:

postParameters.add(new BasicNameValuePair("dateTime", new Long(date.getTime()).toString())); 

Cast it to string. Make sure date is not empty/null (I didn't see in your code that you used the date object with some of the methods from the Date class)

For example:

postParameters.add(new BasicNameValuePair("dateTime", new Long(date.getTime()).toString())); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文