Reputation: 2884
I have a web service written in ASP.NET that accepts a List of type Contact(List).
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
#region List of contacts that need to be created in the database.
public List<KeyValue> createRecordsInDb(List<Contact> dataToPass)
{
return Contact.insertArrayIntoDb(dataToPass);
}
#endregion
On the android device, I am converting a List to a String using the GSON library.
List<Contact> 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();
}
The problem arises when I wish to send the data to the web-service. I'm passing the data through the following method,
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;
}
Whenever I call the webservice, it throws the following error -
{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.List`1
The problem is that data.toString()
returns :-
{"dataToPass":"[{\"address\":\"Himayath Nagar\",\"email\":\"[email protected]\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"[email protected]\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"[email protected]\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]"}
whereas the webservice expects data in the format
{"dataToPass":[{\"address\":\"Himayath Nagar\",\"email\":\"[email protected]\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"[email protected]\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"[email protected]\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]}
I even tried replacing and removing those " " as such :-
data.toString().replace("\"[","[").replace("]\"","]");
(java.lang.String) {"dataToPass":[{\"address\":\"Himayath Nagar\",\"email\":\"[email protected]\",\"name\":\"Abijeet Patro\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"himayath naga\",\"email\":\"[email protected]\",\"name\":\"Saravana\",\"server_id\":\"\",\"status\":\"\"},{\"address\":\"hellpo world\",\"email\":\"[email protected]\",\"name\":\"Syai\",\"server_id\":\"\",\"status\":\"\"}]}
But that does not seem to be working as the Web Server throws an error
{"Message":"Invalid object passed in, member name expected. (16): {\"dataToPass\":[{\\\"address\\\":\\\"Himayath Nagar\\\",\\\"email\\\":\\\"[email protected]\\\",\\\"name\\\":\\\"Abijeet Patro\\\",\\\"server_id\\\":\\\"\\\",\\\"status\\\":\\\"\\\"},{\\\"address\\\":\\\"himayath naga\\\",\\\"email\\\":\\\"[email protected]\\\",\\\"name\\\":\\\"Saravana\\\",\\\"server_id\\\":\\\"\\\",\\\"status\\\":\\\"\\\"},{\\\"address\\\":\\\"hellpo world\\\",\\\"email\\\":\\\"[email protected]\\\",\\\"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"}
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?
Can I use JSON.NET to de-serialize the string to my List?
Upvotes: 1
Views: 729
Reputation: 2884
This link has an answer that worked. I am just getting the data on the server side as string, and then converting it the array that I can process.
Sample JSON:
string json = "[{Name:'John Simith',Age:35},{Name:'Pablo Perez',Age:34}]";
Class:
public class Person
{
public int Age {get;set;}
public string Name {get;set;}
}
Deserialization
JavaScriptSerializer js = new JavaScriptSerializer();
Person [] persons = js.Deserialize<Person[]>(json);
May not be the best answer, since we may not always have access to the code of the Web Server. I'll be leaving this open for a while longer.
Upvotes: 1