C# Api在Postman中运行良好,但在代码Xamarin形式中运行不正常
我在web api中有此请求C# Api在Postman中运行良好,但在代码Xamarin形式中运行不正常,c#,xamarin,asp.net-web-api,xamarin.forms,C#,Xamarin,Asp.net Web Api,Xamarin.forms,我在web api中有此请求 Connection(); SqlCommand cmd = new SqlCommand("[dbo].[SetIDByPhoneNumber]", conn); cmd.Parameters.AddWithValue("@PhoneNumber", student.PhoneNumber); cmd.CommandType = System.Data.CommandT
Connection();
SqlCommand cmd = new SqlCommand("[dbo].[SetIDByPhoneNumber]", conn);
cmd.Parameters.AddWithValue("@PhoneNumber", student.PhoneNumber);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();
int i= cmd.ExecuteNonQuery();
conn.Close();
if (i >= 1)
{
response.Massage = "Found One";
response.Status = 1;
}
else
{
response.Massage = "Nothing -_-";
response.Status = 0;
}
这个Post方法是Xamarin形式的
string url = "MY URL (Which is a real website IP ADDRESS)";
HttpClient client = new HttpClient();
string jsonData = JsonConvert.SerializeObject("Student.PhoneNumber");
StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
string result = await response.Content.ReadAsStringAsync();
Response responseData = JsonConvert.DeserializeObject<Response>(result);
string url=“我的url(真实的网站IP地址)”;
HttpClient=新的HttpClient();
字符串jsonData=JsonConvert.SerializeObject(“Student.PhoneNumber”);
StringContent=新的StringContent(jsonData,Encoding.UTF8,“application/json”);
HttpResponseMessage response=wait client.PostAsync(url、内容);
字符串结果=wait response.Content.ReadAsStringAsync();
Response responseData=JsonConvert.DeserializeObject(结果);
在Android上运行后,我得到“对象引用未设置为对象的实例”和响应0
我不知道是什么问题。我的SQL存储过程工作正常api工作正常。您的
jsonData
不是JSON,请尝试:
string jsonData = JsonConvert.SerializeObject(new { PhoneNumber = "45656" });
我想你的回程票是错的。这样做应该设置适当的头,并且应该将其识别为有效的json
public JsonResult YourAction()
{
// Your Code
return Json(new {student.PhoneNumber});
}
天哪,你救了我的命!我把这个问题考虑了两天。感谢您乐于帮助@MahirMuzahid