I have resolved the problem,
I think my problem is I don't know how to get JSON in webservice .NET
I use android studio for parse JSON Object to my webservice, the JSON Array and JSON Object is working,
but I got a bad request (maybe because I can't get JSON in webservice?)
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
Here is my code from webservice vb.net. I don't know how to get the JSON object
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, UriTemplate:="Change/")>
Function HReceiver(horary As String) As String Implements IService.HReceiver
I suppose I get the JSON with (horary As String) and I try many ways to deserialize,
but it's not the point, because I suppose I can't get 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
Thanks for the time
------------I Resolved With
<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
in Service
<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
and this work without deserializer
in android, I must use
os.writeChars(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
To recive a JSON object in a proper way you have to have a class with the same structure as a function parameter, for example if you are receiving a list of this object you api parameter should be:
Function SpoolsList(objVar As List(Of ArrayHorary)) As IHttpActionResult
Your code
End Function
Then it will parse the object automatically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.