Date data problems encountered when Android posts data to the Rest Service

Source: Internet
Author: User

When I Post data to the Rest service on the Android end today, it is always unsuccessful. I checked a lot of information to know that the time format required for the Rest end to deserialize the json string must be UTC, and Date (12345678 + 0800) format. Android-side serialization method: copy the code // use Gson to serialize the Object to Jsonpublic static String toJson (object Object) {GsonBuilder builder = new GsonBuilder (); // do not convert the field builder without the @ Expose annotation. excludeFieldsWithoutExposeAnnotation (); // register the event builder for the Date type. registerTypeAdapter (Date. class, new UtilDateSerializer (); Gson gson = builder. create (); return gson. toJson (object);} class UtilDateSerializer implements JsonSerializer <Date >{@ Ove Rride public JsonElement serialize (Date src, Type typeOfSrc, JsonSerializationContext context) {// piece together the UTC time Type return new JsonPrimitive ("/Date (" + src. getTime () + "+ 0800 )/");}} copy code Android Post method copy code/*** send request via POST method ** @ param url * URL address * @ param params * parameter * @ return * @ throws Exception */ public String httpPost (String url, string json) throws Exception {String response = null; int timeoutConnection = 3000; int timeoutSocket = 5000; HttpParams httpParameters = new BasicHttpParams (); HttpConnectionParams. setConnectionTimeout (httpParameters, timeoutConnection); HttpConnectionParams. setSoTimeout (httpParameters, timeoutSocket); HttpClient httpClient = new DefaultHttpClient (httpParameters); HttpPost httpPost = new HttpPost (url); // Add http header information httpPost. addHeader ("Content-Type", "application/json"); ht TpPost. addHeader ("User-Agent", "imgfornote"); httpPost. setEntity (new StringEntity (json, "UTF-8"); HttpResponse httpResponse = httpClient.exe cute (httpPost); int statusCode = httpResponse. getStatusLine (). getStatusCode (); if (statusCode = HttpStatus. SC _ OK) {response = EntityUtils. toString (httpResponse. getEntity ();} else {response = String. valueOf (statusCode);} return response;} copy the code C # Rest server [O PerationContract] [WebInvoke (UriTemplate = "/yyxTest", ResponseFormat = WebMessageFormat. json, RequestFormat = WebMessageFormat. json)] string MensarTest (XCJCQK model); A summary of yourself, hoping to help people who encounter the same problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.