2011-08-11 02:08
浏览 65


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


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

// ...

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将Date对象 Date date = new Date()发布到 使用HTTPClient的远程PHP脚本,但似乎 NameValuePair 不接受 String 以外的任何其他Java对象? 如果您能指导我如何使用 HTTPClient 发布 Date 对象,我将不胜感激

这是我的代码 \ ñ

 <代码>的ArrayList&LT;&的NameValuePair GT;  postParameters = new ArrayList&lt; NameValuePair&gt;(); 
Date date = new Date(); 
postParameters.add(new BasicNameValuePair(“stringObj”,“Test”));  //没有错误
postParameters.add(new BasicNameValuePair(“dateTime”,date));  //错误在这里
try {
字符串响应= CustomHttpClient.executeHttpPost(“http://remotewebsite/test.php”,postParameters); 
catch {
 // ... 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dousuize3082 2011-08-11 02:12

    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())); 
    点赞 打赏 评论

相关推荐 更多相似问题