C# 如何接收PHP文件中的json数据?
我是Xamarin和C#的新手,我想从我的应用程序将图书名称和作者插入MySql数据库,所以我创建了一个名为BooksInsert.cs的类:C# 如何接收PHP文件中的json数据?,c#,php,mysql,json,C#,Php,Mysql,Json,我是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; } pu
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>
现在我不知道如何继续将json文件发送到insert.php文件,在insert.php中如何接收json数据,有人能帮我吗?您在
WebHelper.cs
中的Post
方法期待一个Uri
实体作为第一个参数传递到HttpWebRequest.Create
。但是,正如前面所述,它似乎期望一个字符串作为参数,而不是Uri
类。此外,HttpWebRequest
已过时,不应用于新的开发。MS表示您应该使用
请尝试使用WebRequest。改为创建,如本教程中的MS:
如果更改Post
方法签名,则不需要使用NewBookPage.xaml.cs
中的Uri
类,只需将URI作为字符串发送到PostWebHelper.cs
中的Post
方法,它期望一个URI
实体作为第一个参数传递给HttpWebRequest.Create
。但是,正如前面所述,它似乎期望一个字符串作为参数,而不是Uri
类。此外,HttpWebRequest
已过时,不应用于新的开发。MS表示您应该使用
请尝试使用WebRequest。改为创建,如本教程中的MS:
如果您更改Post
方法签名,那么您不需要使用NewBookPage.xaml.cs
中的Uri
类,只需将Uri作为字符串发送到Post
我不知道它是如何正确的,但我尝试了将数据插入MySql数据库的简单方法,我刚刚做了这个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"/>
<WebView x:Name="webView"
WidthRequest="1000"
HeightRequest="500"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
insert.php
$bname=$_GET['bn'];
$bauthor=$_GET['ba'];
$query = "INSERT INTO books VALUES ('','$bname','$bauthor')";
mysqli_query($berikane,$query);
数据插入是否正确,这个解决方案是如何实现的?我不知道它是如何正确的,但我尝试了一种简单的方法将数据插入MySql数据库,我只是做了这个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"/>
<WebView x:Name="webView"
WidthRequest="1000"
HeightRequest="500"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
insert.php
$bname=$_GET['bn'];
$bauthor=$_GET['ba'];
$query = "INSERT INTO books VALUES ('','$bname','$bauthor')";
mysqli_query($berikane,$query);
数据插入正确,这个解决方案是如何实现的?最后,我将Post数据从Xamarin发送到PHP文件,并将其作为Post值接收:
private void Button_Clicked(object sender, EventArgs e)
{
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["book_name"] = b_name.Text;
values["book_auther"] = b_auther.Text;
var response = client.UploadValues("https://MyWeb/insert.php", values);
var responseString = Encoding.Default.GetString(response);
if (response != null)
{
DisplayAlert("Success" ,"Data Inserted Successfully" ,"OK");
}
}
}
最后,我将Post数据从Xamarin发送到PHP文件,并将其作为Post值接收:
private void Button_Clicked(object sender, EventArgs e)
{
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["book_name"] = b_name.Text;
values["book_auther"] = b_auther.Text;
var response = client.UploadValues("https://MyWeb/insert.php", values);
var responseString = Encoding.Default.GetString(response);
if (response != null)
{
DisplayAlert("Success" ,"Data Inserted Successfully" ,"OK");
}
}
}
您不需要向PHP脚本发送JSON文件。如果这些PHP脚本以JSON的形式接收负载,则只需将负载编码为JSON即可。我觉得您已经在这里做了:string request=JsonConvert.SerializeObject(item)代码>在NewBookPage.xaml.cs
中。你测试过你的程序吗?您能否提供进一步的数据,例如请求-响应和错误?你知道insert.php
API吗?@Andre我测试过它,它说uri格式不受支持,那么我如何将数据插入MySql数据库这个php脚本在本地Web服务器上运行吗?您是否尝试将http://
放在URL前面,使其变成http://localhost/API/insert.php
?另外,我会检查它运行的端口,通常是端口80
。http://url没有问题,但现在我只需要如何在insert.php上编辑数据file@AndreRavazzi我在http://,数据库中插入了新行,但没有数据,insert.php ddn无法从应用程序接收数据,如何在insert.phpy中接收数据您不需要将JSON文件发送到PHP脚本。如果这些PHP脚本以JSON的形式接收负载,则只需将负载编码为JSON即可。我觉得您已经在这里做了:string request=JsonConvert.SerializeObject(item)代码>在NewBookPage.xaml.cs
中。你测试过你的程序吗?您能否提供进一步的数据,例如请求-响应和错误?你知道insert.php
API吗?@Andre我测试过它,它说uri格式不受支持,那么我如何将数据插入MySql数据库这个php脚本在本地Web服务器上运行吗?您是否尝试将http://
放在URL前面,使其变成http://localhost/API/insert.php
?另外,我会检查它运行的端口,通常是端口80
。http://url没有问题,但现在我只需要如何在insert.php上编辑数据file@AndreRavazzi我在http://,数据库中插入了新行,但没有数据,insert.php ddn无法从应用程序接收数据,如何在insert.phpYourinsert.php
中接收数据不接收JSON,但接收URL参数。在构建其使用者之前,您应该确认后端的API。您的insert.php
不接收JSON,但接收URL参数。在构建其使用者之前,您应该确认后端的API。