如何在 MVC 上检索 JsonObject 数据
How to retrieve JsonObject data on MVC
我有一个 android 应用程序,它按如下方式将 jsonobject 发送到服务器:
JSONObject jsonParam = new JSONObject();
jsonParam.put("snc", serialClient);
jsonParam.put("code", hashString(pwd + serialClient));
printout = new DataOutputStream(urlConn.getOutputStream());
String str = jsonParam.toString();
byte[] data = str.getBytes("UTF-8");
printout.write(data);
printout.flush();
printout.close ();
MVC控制器如下:
<System.Web.Http.AcceptVerbs("Post")> _
Public Function Android_Index(ByVal data As String) As ActionResult
Dim param As Dictionary(Of String, String) = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(data)
For Each p In param
LogEntry(p.Key & vbCrLf & p.Value, 0, "test")
Next
Return RedirectToLocal("/UI/Forbidden")
End Function
但我收到的数据是空无元素对象。请帮忙找出我哪里出错了,我该如何解决?
更新
我尝试创建一个 MVC class 如下:
Public Class SNC
Public snc As String
Public code As String
End Class
并按如下方式编辑控制器:
Public Function Android_Index(ByVal data As SNC) As ActionResult
LogEntry(data.snc, 0, "test")
LogEntry(data.code, 0, "test")
......
End Function
当 android 应用程序尝试发送 jsonobject 时,我可以确认 Android_Index 已被命中。所以我假设 jsonobject 应该在 "data" 参数中被接受。但是 "snc" 和 "code" 都是空字符串。虽然我从 android 重新检查作为 jsonobject 发送的 "snc" 和 "code" 都不为空。目前,问题似乎出在 android 方面。或者mvc应该接受的方式不正确?
经过几个小时的无所事事,我终于可以让它工作了。
问题在 java 方面:
首先,我必须将 jsonobject 分配给一个键,如下所示:
JSONObject jsonParam = new JSONObject();
jsonParam.put("snc", serialClient);
jsonParam.put("code", hashString(pwd + serialClient));
String param = "data=" + jsonParam.toString(); //assign jsonParam to identifier
然后将内容类型设置为application/x-www-form-urlencoded。
post数据终于被Controller正确捕捉到。
我有一个 android 应用程序,它按如下方式将 jsonobject 发送到服务器:
JSONObject jsonParam = new JSONObject();
jsonParam.put("snc", serialClient);
jsonParam.put("code", hashString(pwd + serialClient));
printout = new DataOutputStream(urlConn.getOutputStream());
String str = jsonParam.toString();
byte[] data = str.getBytes("UTF-8");
printout.write(data);
printout.flush();
printout.close ();
MVC控制器如下:
<System.Web.Http.AcceptVerbs("Post")> _
Public Function Android_Index(ByVal data As String) As ActionResult
Dim param As Dictionary(Of String, String) = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(data)
For Each p In param
LogEntry(p.Key & vbCrLf & p.Value, 0, "test")
Next
Return RedirectToLocal("/UI/Forbidden")
End Function
但我收到的数据是空无元素对象。请帮忙找出我哪里出错了,我该如何解决?
更新
我尝试创建一个 MVC class 如下:
Public Class SNC
Public snc As String
Public code As String
End Class
并按如下方式编辑控制器:
Public Function Android_Index(ByVal data As SNC) As ActionResult
LogEntry(data.snc, 0, "test")
LogEntry(data.code, 0, "test")
......
End Function
当 android 应用程序尝试发送 jsonobject 时,我可以确认 Android_Index 已被命中。所以我假设 jsonobject 应该在 "data" 参数中被接受。但是 "snc" 和 "code" 都是空字符串。虽然我从 android 重新检查作为 jsonobject 发送的 "snc" 和 "code" 都不为空。目前,问题似乎出在 android 方面。或者mvc应该接受的方式不正确?
经过几个小时的无所事事,我终于可以让它工作了。
问题在 java 方面:
首先,我必须将 jsonobject 分配给一个键,如下所示:
JSONObject jsonParam = new JSONObject();
jsonParam.put("snc", serialClient);
jsonParam.put("code", hashString(pwd + serialClient));
String param = "data=" + jsonParam.toString(); //assign jsonParam to identifier
然后将内容类型设置为application/x-www-form-urlencoded。 post数据终于被Controller正确捕捉到。