2015-09-28 47 views
0

我有一個Android應用程序,它發送的JSONObject服務器如下JSONObject的數據:如何檢索MVC

 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 

但我收到的數據空 - 沒有元素對象。請幫忙找到我要出錯的地方,我該如何解決?

UPDATE

我試圖創建一個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應該在「data」參數中被接受。但是「snc」和「code」都是空白字符串。雖然我從android重新檢查,作爲jsonobject發送的「snc」和「code」都不是空的。目前看來,問題出在android方面。或者mvc如何接受的方式不正確?

+1

你的問題是從服務器端還是android端? – dipali

+0

@dipali請參閱更新。我仍試圖追蹤問題的來源。 –

+0

請在log.and打印字符串「str」並檢查它是否創建了一個合適的json?如果你的問題是服務器端代碼,那麼我不能幫你。但如果你的問題是創建json,那麼肯定我會幫你。 – dipali

回答

0

經過幾小時沒有什麼,最後我可以讓它工作。

的問題是在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的形式 - url編碼。 發佈數據最終由Controller正確捕獲。