Note that there are some explanatory texts on larger screens.

plurals
  1. POList of user-defined objects from Android to an ASP.NET webservice through JSON
    primarykey
    data
    text
    <p>I have a web service written in ASP.NET that accepts a List of type Contact(<strong>List</strong>).</p> <pre><code> [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] #region List of contacts that need to be created in the database. public List&lt;KeyValue&gt; createRecordsInDb(List&lt;Contact&gt; dataToPass) { return Contact.insertArrayIntoDb(dataToPass); } #endregion </code></pre> <p>On the android device, I am converting a <strong>List</strong> to a <strong>String</strong> using the GSON library.</p> <pre><code> List&lt;Contact&gt; lstContactsToCreate= retrieveDataFromCursor(cursor,false); String jsonString = new Gson().toJson(lstContactsToCreate); try { callWebService(jsonString, _context.getResources().getString(R.string.updateCreateURL), false); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } </code></pre> <p>The problem arises when I wish to send the data to the web-service. I'm passing the data through the following method,</p> <pre><code>private HttpResponse callWebService(String dataToPass, String URL, boolean isUpdate) throws JSONException, ClientProtocolException, IOException{ HttpPost httpPost = new HttpPost(URL); httpPost.setHeader("content-type", "application/json"); HttpClient httpClient = new DefaultHttpClient(getHttpParameterObj(4000,4000)); // Building the JSON object. JSONObject data = new JSONObject(); data.put("dataToPass", dataToPass); StringEntity entity = new StringEntity(data.toString(), HTTP.UTF_8); entity.setContentType("application/json"); entity.setContentEncoding( "UTF-8"); httpPost.setEntity(entity); // Making the call. HttpResponse response = httpClient.execute(httpPost); return response; } </code></pre> <p>Whenever I call the webservice, it throws the following error - </p> <pre><code>{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.List`1 </code></pre> <p>The problem is that <code>data.toString()</code> returns :-</p> <pre><code>{"dataToPass":"[{\"address\":\"Himayath Nagar\",\"email\":\"abijeet.p@osmosys.asia\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"saravana.e@osmosys.asia\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"asshole@gmail.com\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]"} </code></pre> <p>whereas the webservice expects data in the format</p> <pre><code>{"dataToPass":[{\"address\":\"Himayath Nagar\",\"email\":\"abijeet.p@osmosys.asia\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"saravana.e@osmosys.asia\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"asshole@gmail.com\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]} </code></pre> <p>I even tried replacing and removing those " " as such :- </p> <pre><code>data.toString().replace("\"[","[").replace("]\"","]"); (java.lang.String) {"dataToPass":[{\"address\":\"Himayath Nagar\",\"email\":\"abijeet.p@osmosys.asia\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"saravana.e@osmosys.asia\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"asshole@gmail.com\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]} </code></pre> <p>But that does not seem to be working as the Web Server throws an error</p> <pre><code>{"Message":"Invalid object passed in, member name expected. (16): {\"dataToPass\":[{\\\"address\\\":\\\"Himayath Nagar\\\",\\\"email\\\":\\\"abijeet.p@osmosys.asia\\\",\\\"name\\\":\\\"Abijeet Patro\\\",\\\"server_id\\\":\\\"\\\",\\\"status\\\":\\\"\\\"},{\\\"address\\\":\\\"himayath naga\\\",\\\"email\\\":\\\"saravana.e@osmosys.asia\\\",\\\"name\\\":\\\"Saravana\\\",\\\"server_id\\\":\\\"\\\",\\\"status\\\":\\\"\\\"},{\\\"address\\\":\\\"hellpo world\\\",\\\"email\\\":\\\"asshole@gmail.com\\\",\\\"name\\\":\\\"Syai\\\",\\\"server_id\\\":\\\"\\\",\\\"status\\\":\\\"\\\"}]}","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"} </code></pre> <p>So how do I pass an array of user-defined objects from Java to my ASP.NET web service? Or is there way I can convert the GSON string on the asp.net web server to my List objects?</p> <p>Can I use JSON.NET to de-serialize the string to my List?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload