Android Question HttpUtils2 PostString not passing the JSON string to ASP.NET Web API

chitoq

New Member
Licensed User
Longtime User
Hi, Erel!

I'm not sure what I'm actuall doing wrong. I've created a simple test API to recieve a JSON string and created a simple android app to pass the string using HttpJob.Postring method. However, the method doesn't seem to pass the data to the web api.

The server returns an exception message: Value cannot be null...Parameter name: value

This happens as the API tries to deserialize the JSON string supposedly contained in parameter 'value', which was supposedly sent by the post method.

I've tried consuming the API using a different application and it works fine.


Here's the code that I used based on your example:

B4X:
Sub ButtonPost_Click
    'Send a POST request
    Dim job1 As HttpJob
    job1.Initialize("Job1", Me)
    job1.PostString("http://somesite/testapi/api/values", JSONData)
End Sub

This is the exact JSONData that worked in the other app but not here:
[{"Data2":"test data 2","EmpID":"1234","Data1":"test data 1"},{"Data2":"test data 2","EmpID":"5678","Data1":"test data 1"},{"Data2":"test data 2","EmpID":"9012","Data1":"test data 1"}]

I've also revised the HttpJob code:
B4X:
Public Sub PostBytes(Link As String, Data() As Byte)
    req.InitializePost2(Link, Data)
    'I inserted this line:
    req.SetContentType("application/json")
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

Still to no avail.

Hope you can point me to the right direction as I am at a loss.

Thank you once again for your usual support!
 

chitoq

New Member
Licensed User
Longtime User
Sorry for the late update. Thanks for the reply Erel.

I was able to resolve the problem. I just want to post the solution just in case someone else is also working with ASP.NET Web APIs.

Basically the main culprit was the way the API was setup. It was set to accept posts with generic string data and not limited to JSON object because of specs requirement. I can't believe I actually missed this part.

I've changed the the code to the following:

B4X:
    job1.PostString(..., "=" & JSONData)
job1.GetRequest.SetContentType("application/x-www-form-urlencoded")

The Content Type is no longer "application/json" and I appended "=" to the JSON string when calling the job1.PostString method. For some reason, "value=" doesn't work.

Now, it's working perfectly.

Once again, thanks Erel for this simplified code.
 
Upvote 0

vincentehsu

Member
Licensed User
Longtime User
Dear Chitoq:
could u please share the projct with me? there is no other actually sample code for wcf via json
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
there is no other actually sample code for wcf via json
See my answer here.
If that is not what you are searching for then it would be better to CREATE A NEW THREAD for your problem instead of pulling out old threads.
 
Last edited:
Upvote 0

vincentehsu

Member
Licensed User
Longtime User
Dear DonManfred:
It's my WCF service code as below,the parameter is a Json String,I've been use the download2 function(See B4A Code),the download2's second parameter like Array As String("TXNCode",....) will be auto convert to json string?
I always get the error ,am i miss something?

WCF Service Code
B4X:
Public Function Check_Reginstration(ByVal JsonString As String) As String Implements IMyTranService.Check_Reginstration
        'JsonString CheckRegistrationRequest TXNCode    DeviceID    TeminalID    MAC    EMSK    KSN4MSK    ECSK    KSN4CSK
        '"TXN_REGISTER_R001"
        Dim MyCheckRegistrationRequest As New CheckRegistrationRequest
        MyCheckRegistrationRequest = MyJson.FromJosnT(Of CheckRegistrationRequest)(JsonString)
        Dim cn As New SqlConnection
        cn.ConnectionString = "Data Source=.;Initial Catalog=Poskitz_mPOS_DB_1.6;Integrated Security=True"
        cn.Open()
        Dim dc As New SqlCommand
        dc.Connection = cn
        dc.CommandType = CommandType.StoredProcedure
        dc.CommandText = "uSP_MPA_CheckRegistration"
        dc.Parameters.Clear()
        dc.Parameters.Add(New SqlParameter("@DeviceID", SqlDbType.VarChar))
        dc.Parameters("@DeviceID").Size = 50
        dc.Parameters("@DeviceID").Value = MyCheckRegistrationRequest.DeviceID
        dc.Parameters.Add(New SqlParameter("@TerminalID", SqlDbType.VarChar))
        dc.Parameters("@TerminalID").Size = 50
        dc.Parameters("@TerminalID").Value = MyCheckRegistrationRequest.TerminalID
        dc.Parameters.Add(New SqlParameter("@Ret", SqlDbType.Int))
        dc.Parameters("@Ret").Direction = ParameterDirection.Output
        dc.ExecuteNonQuery()
        Dim MyCheckRegistrationResponse As New CheckRegistrationResponse
        MyCheckRegistrationResponse.TXNCode = "TXN_REGISTER_R001"
        MyCheckRegistrationResponse.MAC = ""
        If dc.Parameters("@Ret").Value = 0 Then
            MyCheckRegistrationResponse.Message = "Registration"
        Else
            MyCheckRegistrationResponse.Message = "Login"
        End If
        Dim Json As String = ""
        'Json = JsonConvert.SerializeObject(MyCheckRegistrationResponse)
        Json = MyJson.ToJosnT(Of CheckRegistrationResponse)(MyCheckRegistrationResponse)
        Return Json
        cn.Close()
        cn.Dispose()
        GC.Collect()
    End Function
B4A Code:
B4X:
job1.Initialize("Job1", Me)
            job1.download2("http://125.227.11.94/Services/MyTranService.svc?singleWsdl", Array As String("TXNCode","TXN_REGISTER_R001","DeviceID","AAA","TerminalID","ST100079","MAC","","EMSK","","ECSK","","KSN4CSK",""))
            job1.GetRequest.Timeout = 6000000

Error Code:
B4X:
** Service (httputils2service) Create **


** Service (httputils2service) Start **


JobName = Job1, Success = true


<HTML><HEAD><link rel="alternate" type="text/xml" href="http://125.227.11.94/Services/MyTranService.svc?disco"/><STYLE type="text/css">#content{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: #000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white}P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdana}PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e0 1px solid; MARGIN-TOP: -5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2em; PADDING-BOTTOM: 5px; BORDER-LEFT: #f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #f0f0e0 1px solid; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e5e5cc}.heading1{MARGIN-TOP: 0px; PADDING-LEFT: 15px; FONT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTTOM: 3px; MARGIN-LEFT: -30px; WIDTH: 100%; COLOR: #ffffff; PADDING-TOP: 10px; FONT-FAMILY: Tahoma; BACKGROUND-COLOR: #003366}.intro{MARGIN-LEFT: -15px}</STYLE><TITLE>MyTranService 服務</TITLE></HEAD><BODY><DIV id="content"><P class="heading1">MyTranService 服務</P><BR/><P class="intro">您已建立服務。<P class='intro'>若要測試此服務,必須建立用戶端,並用它來呼叫服務。請從命令列以下列語法使用 svcutil.exe 工具:</P> <BR/><PRE>svcutil.exe <A HREF="http://125.227.11.94/Services/MyTranService.svc?wsdl">http://125.227.11.94/Services/MyTranService.svc?wsdl</A></PRE><P>您也可以存取服務描述做為單一檔案:<BR/><PRE><A HREF="http://125.227.11.94/Services/MyTranService.svc?singleWsdl">http://125.227.11.94/Services/MyTranService.svc?singleWsdl</A></PRE></P></P><P class="intro"/>這會產生組態檔與包含用戶端類別的程式碼檔。請將這兩個檔案新增至用戶端應用程式,並使用產生的用戶端類別來呼叫服務。例如:<BR/><P class='intro'><B>C#</B></P><PRE><font color="blue">class </font><font color="teal">Test


</font>{
<font color="blue">    static void </font>Main()
    {
        <font color="teal">MyTranServiceClient</font> client = <font color="blue">new </font><font color="teal">MyTranServiceClient</font>();

<font color="green">        // 使用 'client' 變數來呼叫服務上的作業。

</font><font color="green">        // 永遠關閉用戶端。
</font>        client.Close();
    }
}
</PRE><BR/><P class='intro'><B>Visual Basic</B></P><PRE><font color="blue">Class </font><font color="teal">Test
</font><font color="blue">    Shared Sub </font>Main()
<font color="blue">        Dim </font>client As <font color="teal">MyTranServiceClient</font> = <font color="blue">New </font><font color="teal">MyTranServiceClient</font>()
<font color="green">        ' 使用 'client' 變數來呼叫服務上的作業。

</font><font color="green">        ' 永遠關閉用戶端。
</font>        client.Close()
<font color="blue">    End Sub
</font><font color="blue">End Class</font></PRE></DIV></BODY></HTML>
 
Upvote 0
Top