2017-05-24 4 views
0

を使ってPHPと通信するので、これはXamarin androidプロジェクトのためにできるだけ簡単にしたいので、webrequestとphpを使ってmysqlデータベースにデータを挿入する方法のチュートリアルに従います。ここでWebRequestを使ってC#

はCRUD

のために他の私は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スクリプト を介して入力を取得し、私の活動コネクションのための1

<?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; 
    } 
} 
?> 

です

<?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時間すべてを試しました。あなたの助けを事前に感謝

+0

あなたの問題は正確には、2つのサーバーサイド言語があります。どちらかが有効なWebリクエストを作成する場合、もう一方はそれを受け入れる必要があります。 – Greg

+0

それは私がどこでうんざりするのかわからない問題です。私は自分のPHPスクリプトにエラーがないと確信しています.WebRequestにはまだ慣れていません。正しく使用すればわかりません。 – Geeksan

+0

https://msdn.microsoft .com/en-us/library/system.net.webrequest(v = vs.110).aspx – Greg

答えて

1

簡単な解決策はあなただけで行うあなたのURLを呼び出したい場合は、

はその直後$ _GET

と$ _POSTを置き換える得るためにポストからあなたのphpを変更することであろうこの例のように

http://localhost/CRUDSERVICE/CreateContact.php?Nom=Doe&Prenom=Joe&Tel=025545&Dep=Security&Com=somecom&Adress=street431&[email protected]

は、今度は、コード

String prenom="john"; 
String nom="doe"; 
String tel="025545"; 
String dep="security"; 
String com="somecom"; 
String adress="street431"; 
String courriel="[email protected]"; 
// let's build our link 
String link="http://localhost/CRUDSERVICE/CreateContact.php?Nom="+nom+"&Prenom="+prenom+"&Tel="+tel+"&Dep="+dep+"&Com="+com+"&Adress+"+adress+"&Courriel="+courriel; 
WebClient wc=new WebClient(); // don't forget using System.Net; 
String res=wc.DownloadString(link); // res is data printed by echo 
if(res=="No Connection " || res=="Query Failed") 
{ 
    // something went wrong 
} 
else 
{ 

} 
のために手放す
関連する問題