Json 如何使用HttpClient发布到Web Api以注册用户
我正在进行基于Wifi的本地测试: 1) 我的开发PC和Android手机连接到同一个WifiJson 如何使用HttpClient发布到Web Api以注册用户,json,xamarin.android,async-await,Json,Xamarin.android,Async Await,我正在进行基于Wifi的本地测试: 1) 我的开发PC和Android手机连接到同一个Wifi 2) Android手机已连接到开发PC 3) Web Api应用程序和Xamarin android应用程序都在此开发PC中 4) 已设置电脑的静态IP地址,如下代码所示。 问题: 我正在使用Android应用程序中的HttpClient在基于WiFi连接的本地测试环境中发布Asp.net Web Api。 我的目标是注册一个用户,我期望像OK这样的HttpStatusCode和来自Web Api的
2) Android手机已连接到开发PC
3) Web Api应用程序和Xamarin android应用程序都在此开发PC中
4) 已设置电脑的静态IP地址,如下代码所示。
问题:
我正在使用Android应用程序中的HttpClient在基于WiFi连接的本地测试环境中发布Asp.net Web Api。
我的目标是注册一个用户,我期望像OK这样的HttpStatusCode和来自Web Api的用户对象作为HttpResponseMessage 但是,下面的代码不会返回结果 因此,我有以下问题: 1) JsonConvert是否可以使用Usr Obj var json=JsonConvert.SerializeObject(Usr) 2) 这个DateTime.Today可以在new User()类中使用吗 3) 是否正确使用了Async和Wait 代码:
[Activity(Label = "HttpClientPostApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
Button HttpBtn;
TextView txtView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle)
SetContentView(Resource.Layout.Main);
HttpBtn = FindViewById<Button>(Resource.Id.button1);
txtView = FindViewById<TextView>(Resource.Id.tv1);
HandleEvents();
}
private void HandleEvents()
{
HttpBtn.Click += HttpBtn_Click;
}
private async void HttpBtn_Click(object sender, EventArgs e)
{
try
{
string str = await Register();
}
catch (Exception ex)
{
String str = ex.ToString();
}
}
private async Task<string> Register()
{
try
{
var Usr = new User()
{
UserName = "User1",
UserPassword = "1234567",
UserEmailAddr = "r@m.com",
UserMobileNo = "123456789",
CreatedOn = DateTime.Today
};
HttpClient client = new HttpClient();
var url = "http://192.168.1.3:9484/api/User";
var uri = new Uri(url);
//-- set up header
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var json = JsonConvert.SerializeObject(Usr);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
var responceString = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
txtView.Text = responceString.ToString();
return responceString.ToString();
}
return string.Empty;
}
catch (Exception ex)
{
String str = ex.ToString();
}
}
}
when system run at this point. it shows the Frame not in module.
HttpClient client = new HttpClient();
-- the out put error i can get at the bottom is below
05-04 14:44:10.043 D/Mono (20418): Probing 'monodroid_get_system_property'.
05-04 14:44:10.043 D/Mono (20418): Found as 'monodroid_get_system_property'.
05-04 14:44:10.043 D/Mono (20418): DllImport searching in: '__Internal' ('(null)').
05-04 14:44:10.043 D/Mono (20418): Searching for 'monodroid_free'.
05-04 14:44:10.043 D/Mono (20418): Probing 'monodroid_free'.
05-04 14:44:10.043 D/Mono (20418): Found as 'monodroid_free'.
非常感谢你的帮助
谢谢你的问题是什么?你预期的反应是什么?请用问题更新您的问题。此外,您还需要等待调用
ReadAsStringAsync
,而不是阻塞.Result
。如何从asp.net web api获取用户和HttpStatusCode返回?谢谢(1)是,(2)是,(3)否-不要用阻塞。结果等待呼叫。不清楚为什么不将json字符串反序列化为用户
,但这取决于您。为了找出为什么你什么也没拿回来,在你的控制器的post方法中设置一个断点,并从那里遵循逻辑。另外,如果您检查两次状态代码,ensureccessstatuscode
如果结果不成功,将引发异常。我已更新了代码。我使用反序列化obj。我不明白你的意思。我已经在Web Api中设置了断点,但在Android应用程序上单击提交按钮后没有任何迹象。如果您能更正我的代码,我将不胜感激。如果您在web服务中调用的方法的第一行上有一个断点,并且在调试模式下它没有被命中,那么您就不会调用该方法。响应的状态代码是什么?是否引发异常?最后,此行response.Content.ReadAsStringAsync().Result
是一个阻塞调用,将其更改为wait response.Content.ReadAsStringAsync()
。
when system run at this point. it shows the Frame not in module.
HttpClient client = new HttpClient();
-- the out put error i can get at the bottom is below
05-04 14:44:10.043 D/Mono (20418): Probing 'monodroid_get_system_property'.
05-04 14:44:10.043 D/Mono (20418): Found as 'monodroid_get_system_property'.
05-04 14:44:10.043 D/Mono (20418): DllImport searching in: '__Internal' ('(null)').
05-04 14:44:10.043 D/Mono (20418): Searching for 'monodroid_free'.
05-04 14:44:10.043 D/Mono (20418): Probing 'monodroid_free'.
05-04 14:44:10.043 D/Mono (20418): Found as 'monodroid_free'.