2011-08-11 25 views
1

我想使用HTTPClient將日期對象Date date = new Date()發佈到遠程PHP腳本,但好像NameValuePair不接受除String以外的任何其他Java對象?將不勝感激,如果你能指導我如何使用後HTTPClientAndroid:使用HTTPClient將日期對象發佈到PHP

一個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{ 
// ... 
} 

回答

1

演員串。確保date不爲空/空

例如(我沒有在你的代碼,您使用的date對象的一些從Date類的方法看):

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

我知道我可以將其轉換爲字符串。但是沒有辦法直接傳入日期對象嗎? – ZXingIT

+0

不可以,因爲'BNVP'構造函數只接受'values'的字符串。 –

相關問題