【发布时间】:2020-04-18 21:30:52
【问题描述】:
我已经解决了问题,
我认为我的问题是我不知道如何在 webservice .NET 中获取 JSON
我使用 android studio 将 JSON 对象解析到我的网络服务,JSON 数组和 JSON 对象正在工作,
但我收到了一个错误的请求(可能是因为我无法在 web 服务中获取 JSON?)
JSONObject jsonObject = new JSONObject();
jsonObject.put("horary", jsonArray);
URL url = new URL("URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(4000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept","application/json");
conn.setDoOutput(true);
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.writeBytes(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
os.flush();
os.close();
Log.d("json", conn.getResponseMessage());<- bad Request
这是我来自 webservice vb.net 的代码。我不知道如何获取 JSON 对象
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, UriTemplate:="Change/")>
Function HReceiver(horary As String) As String Implements IService.HReceiver
我想我用 (horary As String) 得到 JSON,我尝试了很多方法来反序列化,
但这不是重点,因为我想我无法获取 JSON
Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
Dim horary As ArrayHorary
Dim result As String
Try
Dim items = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ArrayHorary())(horary)
result = horary.ToString
Catch ex As Exception
result = ex.ToString
End Try
Return result
End Function
Public Class ArrayHorary
Public Property id As String
Public Property name As String
End Class
感谢您的时间
------------我解决了
<OperationContract()>
Function HReceiver(horary As Horar) As String
<DataContract()>
Public Class Horar
<DataMember()>
Public Property horary As List(Of Horari)
End Class
<DataContract()>
Public Class Horari
<DataMember()>
Public Property id As String
<DataMember()>
Public Property name As String
End Class
服务中
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, UriTemplate:="Change/")>
Function HReceiver(horar As Horar) As String Implements IService.HReceiver
Dim result As String
result = horar.horary(0).name
return result
而且这项工作没有反序列化器
在android中,我必须使用
os.writeChars(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
【问题讨论】:
标签: java android json vb.net wcf