我在Xamarin工作室尝试下面的代码,构建一个解决方案是没有错误,运行并在Android模拟器上测试它,但没有获取数据库中的数据。并且还启用了互联网许可。
处理呼叫休息服务的代码
using RestSharp;
...
using System.Json;
namespace DemoAndroid
{
public class RestClass
{
public void insert(string text1, string text2, string text3, string text4, int text5)
{
var client = new RestClient("http://192.168.1.34/test/v1/");
var request = new RestRequest("postdata", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new
{
name = text1,
address= text2,
city = text3,
country = text4,
uid = text5
});
client.Execute(request);
}
}
}
发布数据的主要活动类
namespace DemoAndroid
{
[Activity (Label = "DemoAndroid", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
RestClass aa = new RestClass();
EditText editText1, editText2, editText3, editText4, editText5;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
Button button1 = FindViewById<Button> (Resource.Id.button1);
editText1 = FindViewById<EditText> (Resource.Id.editText1);
editText2 = FindViewById<EditText> (Resource.Id.editText2);
editText3 = FindViewById<EditText> (Resource.Id.editText3);
editText4 = FindViewById<EditText> (Resource.Id.editText4);
editText5 = FindViewById<EditText> (Resource.Id.editText5);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
button1.Click += delegate {
aa.insert(editText1.Text, editText2.Text, editText3.Text, editText4.Text, int.Parse(editText5.Text));
}
}
}
}
这是api链接的示例和发布数据的参数
http://192.168.1.34/test/v1/postdata
name, address, city, country, uid
我的api在任何其他客户端应用程序中工作正常,在成功插入后给出此响应
{
message: "successfully inserted"
}
此外,我尝试在点击时显示消息响应是否已插入数据,请在建议我如何执行此操作以及为什么发布数据无法插入数据库中。 我在许多站点的示例android中使用Restsharp检查发布数据,但无法找到。 谢谢