Я новичок в Xamarin и C#, я хочу из своего приложения вставить имя и автора книг в базу данных MySql, поэтому я создал класс с именем BooksInsert.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace NewTest.Model
{
class BooksInsert
{
public string book_name { get; set; }
public string book_auther { get; set; }
}
}
затем другой класс с именем WebHelper.cs для GET и POST:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
using System.Net;
namespace NewTest.Model
{
class WebHelper
{
public string Post(Uri url, string value)
{
var request = HttpWebRequest.Create(url);
var byteData = Encoding.ASCII.GetBytes(value);
request.ContentType = "application/json";
request.Method = "POST";
try
{
using (var stream = request.GetRequestStream())
{
stream.Write(byteData, 0, byteData.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
catch (WebException)
{
return null;
}
}
public string Get(Uri url)
{
var request = HttpWebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "GET";
try
{
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
catch (WebException)
{
return null;
}
}
}
}
на странице добавления NewBookPage.xaml У меня есть это содержимое:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="NewTest.NewBookPage">
<ContentPage.Content>
<StackLayout>
<Entry x:Name="b_name" Placeholder="Name of Book"/>
<Entry x:Name="b_auther" Placeholder="auther of Book"/>
<Button Text="Save"
Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
и NewBookPage.xaml.cs:
using NewTest.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace NewTest
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NewBookPage : ContentPage
{
public NewBookPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
WebHelper webHelper = new WebHelper();
BooksInsert item = new BooksInsert();
item.book_name= b_name.Text;
item.book_auther = b_auther.Text;
string request = JsonConvert.SerializeObject(item);
Uri url = new Uri(string.Format("localhost/API/insert.php"));
string response = webHelper.Post(url, request);
if (response != null)
{
//Handle your reponse here
}
else
{
//No Response from the server
}
}
}
}
Теперь я не знаю, как продолжить отправку json файла для вставки. php файл и вставки. php как я могу получить json данные, может ли кто-нибудь помочь мне