【问题标题】:How to retrieve JsonObject data on MVC如何在 MVC 上检索 JsonObject 数据
【发布时间】:2015-12-25 05:53:45
【问题描述】:

我有一个 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 类如下:

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 应该在“数据”参数中接受。但是“snc”和“code”都是空字符串。虽然我从 android 重新检查了作为 jsonobject 发送的“snc”和“code”都不为空。目前,问题似乎出在android方面。或者mvc应该如何接受的方式不正确?

【问题讨论】:

  • 你的问题是服务器端还是安卓端?
  • @dipali 请查看更新。我仍然试图追查问题出在哪里。
  • 请在日志中打印字符串“str”。并检查它是否创建了正确的 json?如果您的问题是服务器端代码,那么我无法帮助您。但如果您的问题是创建 json,那我一定会帮你的。
  • @dipali,字符串str值为{"snc":"6MYGLSZR9G1PJCUE","code":"Rqo0bfN7Hdlt5vR4rn4S9ERHw\/4="}
  • 你的 Json 是有效的。所以你的问题是服务器端代码。

标签: android json model-view-controller jsonobject


【解决方案1】:

经过几个小时一无所获,我终于可以让它工作了。

问题出在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

然后将 Content-Type 设置为 application/x-www-form-urlencoded。 帖子数据终于被Controller正确捕捉到了。

【讨论】:

    猜你喜欢
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2015-10-12
    • 2021-03-04
    • 1970-01-01
    • 2020-08-08
    相关资源
    最近更新 更多