POSTing DateTime from Android to WCF RESTful JSON service

I am trying to send a DateTime as a parameter to a method opened through a WCF RESTful service with JSON encoding. The request is as follows:

 POST http://IP:PORT/LogService/json/GetLogEntriesByModule HTTP/1.1 Content-Length: 100 Content-Type: application/json Host: IP:PORT Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4) Expect: 100-Continue {"maxentries":10,"upperdate":"1280703601462","lowerdate":"1277938801462","module":"Windows Service"} 

I tried several formats for DateTime :

  • 2010-07-01T10:54:00 (which is sent by the WCFTestClient application on top of NET.TCP and receives the results
  • \/Date(12345678+0100)\/
  • 01.07.2010 10:54:00

Method Definition:

 LogEntry[] GetLogEntriesByModule( string module, DateTime lowerDate, DateTime upperDate, int maxEntries, out bool maxEntriesReached ) 

I always get the following answer:

 HTTP/1.1 200 OK Content-Length: 60 Content-Type: application/json; charset=utf-8 Server: Microsoft-HTTPAPI/2.0 Date: Fri, 02 Jul 2010 09:07:04 GMT {"GetLogEntriesByModuleResult":[],"maxEntriesReached":false} 

It seems that DateTime parsed incorrectly because there are several entries in the Llog at this time.

Does anyone know how to do this?

Refresh . The problem was server side and was resolved.

+4
source share
4 answers

The correct format for sending dates to the WCF service is: /Date(53244000000)/ where the number in brackets is the number of milliseconds since 1970 UTC Midnight.

 Date dt = new Date(); long date = Date.UTC(dt.getYear(), dt.getMonth(), dt.getDay(), dt.getHours(),dt.getMinutes(), dt.getSeconds()); String senddate = "/date("+date+")/"; 

And then use it as below

 inputparam.put("DateTime", datetime); 
+7
source

Just in case, this helps someone:

According to Microsoft, WCF uses a QueryStringConverter to parse URLs. Thus, using this code (a small modification to the msdn example):

 QueryStringConverter converter = new QueryStringConverter(); if (converter.CanConvert(typeof(DateTime))) { string strValue = converter.ConvertValueToString(DateTime.UtcNow, typeof(DateTime)); Console.WriteLine("the value = {0}", strValue); } 

we get the correct parameter format DateTime REST: 2010-01-01T01: 01: 01Z

I read that you tried this format, but just wanted to confirm that it really is how it should work. I thought about answering as this question arose first when I searched for it.

This worked for me using .NET 4.0

+2
source

I need to update the answer to this question, I needed to do the same, and I used the accepted answer, but the received date was incorrect, it gave me the date of the week earlier (I don’t know why). I know little about the Date type of a Java type, but I know that some of their methods are deprecated.

They say that I think this is the last right answer for this situation. Hope helps someone else

 import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; ..... final Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT")); cal.setTime(value); // Where Value is a Date final long date = cal.getTime().getTime(); final String senddate = "/Date("+date+")/"; 

Note the use of dates using Upper case "D" , this is also necessary, as it causes problems with WCF (at least in my case, a lot of stuff is stuck for lowercase D)

I use it to use RESTful WCF with the Jackson library for JSON.

To add a complete answer, you can use this method for the date you want to serialize using jackson.

 import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.annotate.JsonCreator; import org.codehaus.jackson.map.SerializerProvider; import org.codehaus.jackson.map.ser.std.SerializerBase; /** * @author HECTOR * */ public class SerializeRESTDate extends SerializerBase<Date> { public SerializeRESTDate() { // TODO Auto-generated constructor stub this(Date.class); } @JsonCreator protected SerializeRESTDate(Class<Date> t) { super(t); // TODO Auto-generated constructor stub } @Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { final Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT")); cal.setTime(value); final long date = cal.getTime().getTime(); final String senddate = "/Date("+date+")/"; jgen.writeString(senddate); } } 

And use it like in class class definition

 @JsonSerialize(using = SerializeRESTDate.class) @JsonProperty("InspectionDate") /** * @return the _InspectionDate */ public Date get_InspectionDate() { return _InspectionDate; } 
0
source

This is the code that I use to send from Android Java to .NET WebApp. Create a JSON object with a date in the W3C XML Schema:

 Calendar myCalendarObj = Calendar.getInstance(); // Snap a moment in time JSONObject jObj = new JSONObject(); // Create a JSON object (org.json.JSONObject) SimpleDateFormat jsonDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // from java.text.SimpleDateFormat String senddate = jsonDateFormat.format(myCalendarObj.getTime()); jObj.put("a_date_field", senddate); 
0
source

Source: https://habr.com/ru/post/1314525/


All Articles