1

我有一个用 ASP.NET 编写的 Web 服务,它接受 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

在 android 设备上,我使用 GSON 库将List转换为String 。

    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();
    }

当我希望将数据发送到 Web 服务时,就会出现问题。我通过以下方法传递数据,

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;
}

每当我调用网络服务时,它都会引发以下错误 -

{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.List`1

问题是data.toString()返回:-

{"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\":\"\"}]"}

而网络服务需要格式的数据

{"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\":\"\"}]}

我什至尝试替换和删除那些“”:-

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\":\"\"}]}

但这似乎不起作用,因为 Web 服务器会引发错误

{"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"}

那么如何将一组用户定义的对象从 Java 传递到我的 ASP.NET Web 服务?或者有没有办法可以将 asp.net Web 服务器上的 GSON 字符串转换为我的 List 对象?

我可以使用 JSON.NET 将字符串反序列化到我的列表吗?

4

1 回答 1

1

这个链接有一个有效的答案。我只是将服务器端的数据作为字符串获取,然后将其转换为我可以处理的数组。

示例 JSON:

string json = "[{Name:'John Simith',Age:35},{Name:'Pablo Perez',Age:34}]"; 

班级:

public class Person
{
 public int Age {get;set;}
 public string Name {get;set;}
}

反序列化

JavaScriptSerializer js = new JavaScriptSerializer();
Person [] persons =  js.Deserialize<Person[]>(json);

可能不是最好的答案,因为我们可能并不总是可以访问 Web 服务器的代码。我将把它打开一段时间。

于 2013-06-05T10:10:07.210 回答