2016-05-11 7 views
0

私はPHPを使用してRest APIを作成することに新しいです。例えばRest API GETメソッドVisual Studio 2015とXamarinを使用

// connect to the mysql database 
$link = mysql_connect('localhost', 'root', 'pass'); 
mysql_select_db('database',$link); 

// retrieve the table and key from the path 
$nama_tujuan = preg_replace('/[^a-z0-9_]+/i','',array_shift($request)); 
$nama_tabel = preg_replace('/[^a-z0-9_]+/i','',array_shift($request)); 
//Menu Login 
if($nama_tujuan=="login") 
{ 

$username = preg_replace('/[^a-z0-9_]+/i','',array_shift($request)); 
$password = preg_replace('/[^a-z0-9_]+/i','',array_shift($request)); 
    if($method=='GET') 
    { 
     $sql = "select * from $nama_tabel where username_reg='$username' and pass_reg='$password'"; 
     $result = mysql_query($sql,$link); 
     if (!$result) { 
      http_response_code(404); 
      die(mysql_error()); 
     } 
       $cek_user=mysql_num_rows($result); 
       //cek user 
       if($cek_user==1) 
       { 
        //Kirim data yang dperlukan 
        for ($i=0;$i<mysql_num_rows($result);$i++) 
         { 
          echo ($i>0?',':'').json_encode(mysql_fetch_object($result)); 
         } 
       } 
    } 
} 

は、私はそれを試してください:

http://localhost/api.php/login/member/neo/qw 

と、それはすべてのフィールドを表示し、私は私のコードは動作しますが、私はそれについてはよく分からない、1を作成する必要があり、ここのコードですこのように:

{"nik":"46464646457349187","username_reg":"neo","nm_dpn_reg":"neo","nm_blg_reg":"alit","email_reg":"[email protected]","pass_reg":"qw","jns_kel":"P","pin_reg":"111111","tmp_lahir_reg":"lumajang","tgl_lahir_reg":"1-1-1945","alamat_reg":"jolotundo","kota_reg":"surabaya","kd_pos":"60131","propinsi":"Aceh","alamat_kirim":"","kota_kirim":"","kdpos_kirim":"","propinsi_kirim":"","wilayah":"jawa","tlp_hp_reg":"08964507555","tgl_daf":"2016-04-27 16:32:00","ikut_resel":"","bonus":"0"} 

これはRest APIと呼ばれていますか?そして私の2番目の質問は、私はAndroidアプリでJSONデータを取得したい、私はVisual Studio 2015とXamarinを使用しています。私はすでにいくつかのコードを作成しますが、残りの部分はわかりません。なぜなら、私はAndroidアプリケーションを開発することに新しいからです。ここに私の活動のコードは次のとおりです。事前に

   button.Click += async (sender, e) => { 

      string url = "http://localhost/api.php/login/login_mbr/" + 
         txtUsername.Text + 
         "/" + 
         txtPassword.Text; 


      string m = JsonConvert.DeserializeObject<string>(await FetchWeatherAsync(url)); 
      // ParseAndDisplay (json); 
     }; 
    } 
    private async Task<string> FetchWeatherAsync(string url) 
    { 
     // Create an HTTP web request using the URL: 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); 
     request.ContentType = "application/json"; 
     request.Method = "GET"; 

    } 

感謝:)

答えて

2

あなたは、このことにより、JSON文字列を取得できます。

private async Task<string> FetchWeatherAsync (string url) 
{ 
    // Create an HTTP web request using the URL: 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url)); 
    request.ContentType = "application/json"; 
    request.Method = "GET"; 

    // Send the request to the server and wait for the response: 
    using (WebResponse response = await request.GetResponseAsync()) 
    { 
     // Get a stream representation of the HTTP web response: 
     using (Stream stream = response.GetResponseStream()) 
     { 
      string strContent = sr.ReadToEnd(); 
      return strContent; 
     } 
    } 
} 

今、これはすでにあなたにJSONを返します。

あなたがオブジェクトにJSONをデシリアライズしたい場合は以下のように指定されたプロパティを持つオブジェクトを作成する必要があります。「

public class WeatherResult() 
{ 
    public string nik {get; set;} 

     public string username_reg {get; set;} 
    ... 
    ... 
    ... 
} 

、あなたはドンなら、あなたは

WeatherObject result = JsonConvert.DeserializeObject<WeatherResult>(await FetchWeatherAsync(url)); 

によってそれを得ることができます動的オブジェクトを使用できる新しいオブジェクトを作成したい

dynamic result = JObject.Parse(await FetchWeatherAsync(url)); 

注:0123が必要ですJSON用ライブラリ

+0

あなたは私の一日を作った! 私はそれを取得するために、指定されたプロパティを持つオブジェクトを作成します。ありがとうございます:D – neneo

+0

ようこそ。答えがあなたの問題を解決したら、それを受け入れることを検討してください。 – Maarty

+0

しかしそれは待っている、プロセスの終わりまで待ってから次の方法を行う必要があります。私は何かを逃したのですか? – neneo

関連する問題