0
這是一個Xamarin android項目,我想盡可能使它儘可能簡單,因此我遵循關於如何使用webrequest和php將數據插入到mysql數據庫的教程。使用WebRequest使用C#與0123進行通信使用C#
這裏是我的活動,我檢索通過editTexts
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
namespace CPDEP1
{
[Activity(Label = "Formulaire d'enregistrement", Theme="@style/Theme.AppCompat.Light")]
public class FormActivity : AppCompatActivity
{
CoordinatorLayout FrootLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
FrootLayout = FindViewById<CoordinatorLayout>(Resource.Id.FcoordLayout);
SetContentView(Resource.Layout.Form);
var inputNom = FindViewById<EditText>(Resource.Id.nom);
var inputPrenom = FindViewById<EditText>(Resource.Id.pren);
var inputTel = FindViewById<EditText>(Resource.Id.tel);
var inputID = FindViewById<EditText>(Resource.Id.iden);
var inputDep = FindViewById<EditText>(Resource.Id.dep);
var inputCom = FindViewById<EditText>(Resource.Id.com);
var savebtn = FindViewById<Button>(Resource.Id.save);
var inputAdresse = FindViewById<EditText>(Resource.Id.adresse);
var inputCourriel = FindViewById<EditText>(Resource.Id.email);
savebtn.Click += delegate
{
string nom = inputNom.Text;
string prenom = inputPrenom.Text;
string tel = inputTel.Text;
string id = inputID.Text;
string dep = inputDep.Text;
string com = inputCom.Text;
string adress = inputAdresse.Text;
string courriel = inputCourriel.Text;
//REceiving value from an activity
//string user = Intent.GetStringExtra("User");
//string pass = Intent.GetStringExtra("Password");
if (nom == null || prenom == null || tel == null || id == null || dep == null || com == null || adress == null || courriel == null)
{
////////////SSNACKBAR///////////
Snackbar snack = Snackbar.Make(FrootLayout, "Empty Field", Snackbar.LengthShort)
.SetAction("", (view) =>
{
Toast.MakeText(this, "OK", ToastLength.Short).Show();
}).SetActionTextColor(Color.ParseColor("#000000"));
View SnackView = snack.View;
SnackView.SetBackgroundColor(Color.ParseColor("#E72222"));
snack.Show();
}
else
{
WebClient client = new WebClient();
Uri uri = new Uri("http://localhost/CRUDSERVICE/CreateContact.php");
NameValueCollection parameters = new NameValueCollection();
parameters.Add("Nom", nom);
parameters.Add("Prenom", prenom);
parameters.Add("Tel", tel);
parameters.Add("Dep", dep);
parameters.Add("Com", com);
parameters.Add("Adress", adress);
parameters.Add("Courriel", courriel);
client.UploadValuesAsync(uri, parameters);
}
};
}
// }
}
輸入}
而且PHP腳本 一個用於聯接
<?php
class ConnectionInfo
{
public $mServerName;
public $mConnectionInfo;
public $conn;
public function GetConnection()
{
$this->mServerName = 'localhost';
$this->mConnectionInfo = array("Database"=>"formation", "UID"=>"root", "PWD"=>"cpdep");
$this->conn = sqlsrv_connect($this->mServerName,$this->mConnectionInfo);
return $this->conn;
}
}
?>
其他的CRUD
<?php
require_once(dirname(__FILE__).'/ConnectionInfo.php');
if (isset($_POST['Nom']) && isset($_POST['Prenom']) && isset($_POST['Tel']) && isset($_POST['Dep']) && isset($_POST['Com']) && isset($_POST['Adress']) && isset($_POST['Courriel']))
{
//Get the POST variables
$mNom = $_POST['Nom'];
$mPrenom = $_POST['Prenom'];
$mTel = $_POST['Tel'];
$mDep = $_POST['Dep'];
$mCom = $_POST['Com'];
$mAdress = $_POST['Adress'];
$mCourriel = $_POST['Courriel'];
//Set up our connection
$connectionInfo = new ConnectionInfo();
$connectionInfo->GetConnection();
if (!$connectionInfo->conn)
{
//Connection failed
echo 'No Connection';
}
else
{
//Insert new contact into database
$query = 'INSERT INTO thinktank (nom, prenom, telephone, departement, commune, adresse, courriel) VALUES (?, ?, ?, ?, ?, ?, ?)';
$parameters = array($mNom, $mPrenom, $mTel, $mDep, $mCom, $mAdress, $mCourriel);
//Execute query
$stmt = sqlsrv_query($connectionInfo->conn, $query, $parameters);
if (!$stmt)
{ //The query failed
echo 'Query Failed';
}
else
{
//The query succeeded, now echo back the new contact ID
$query = "SELECT IDENT_CURRENT('Contacts') AS NewID";
$stmt = sqlsrv_query($connectionInfo->conn, $query);
$row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
echo $row['NewID'];
}
}
}
?>
過去3個小時我都試過了。在此先感謝您的幫助
你的問題到底是什麼,你有兩種服務器端語言。如果其中一方制定了有效的網絡請求,另一方應該能夠接受它。 – Greg
這就是問題我不知道我在哪裏搞砸了,我只知道我的php腳本沒有錯誤,我對WebRequest還不熟悉,但我不確定是否正確使用它 – Geeksan
https://msdn.microsoft .COM/EN-US /庫/ system.net.webrequest(v = vs.110)的.aspx – Greg