使用Json将DateTime作为android客户端和WCF服务之间的参数传递

时间:2012-11-09 16:19:13

标签: java android json wcf rest

我正在尝试从WCF服务获取使用android作为客户端的DateTime(Joda DateTime)过滤的对象列表。我正在使用Json和REST来处理请求。

如何将日期时间值作为参数传递?

类似的东西:

HttpPost request = new HttpPost( SERVICE_URI + "/GetScheduleEntrysByDate/" + date.toString());

而且:

[OperationContract]
[WebInvoke(Method = "POST",
    UriTemplate = "GetScheduleEntrysByDate/{date}",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
List<ScheduleEntry> GetScheduleEntrysByDate(DateTime date);

1 个答案:

答案 0 :(得分:0)

// you might wanna specify custom params here for the DefaultHttpClient contructor
DefaultHttpClient httpClient = new DefaultHttpClient();

HttpPost request = new HttpPost( SERVICE_URI + "/GetScheduleEntrysByDate/" + date.toString());

List<NameValuePair> bodyParams = new ArrayList<NameValuePair>();
bodyParams.add(new BasicNameValuePair("date", new Date().getTime());
if (bodyParams.size() > 0) {
    try {
        // Include the request body
        post.setEntity(new UrlEncodedFormEntity(bodyParams));
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("Body parameters produced unsupported encoding?", e);
    }
}
// sends the POST with params,  you will need to put this in a try catch
try {
    HttpResponse httpResponse = httpClient.execute(request);
} catch (Exception e){
}