从MySQL数据库中检索数据并将其显示在listiview中

时间:2016-06-21 21:37:26

标签: php android mysql database xamarin

我正在尝试从Microsoft Azure中托管的MySQL数据库中检索数据,并将其显示在列表视图中(在片段中)。 我遵循了这篇文章的指示:

Retrieving data from Mysql DB with PHP and JSON in Xamarin Android C#.NET

但我仍然得到一张空白名单。我认为错误来自我的PHP,因为当我运行我的PHP脚本时,我的浏览器中什么都没有。 这是我的代码:

PHP:

 <?php
  ini_set('display_errors', 1); 
  error_reporting(E_ALL);

  $servername = "eu-cdbr-azure-north-e.cloudapp.net";
  $username = "****************";
  $password = "**********";

 try 
 { 
 $conn = new PDO("mysql:host=$servername;dbname=******",$username,        $password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 }
 catch(PDOException $e)
 {
echo "Connection failed: " . $e->getMessage();
 }

  $query = $conn->prepare("SELECT FROM contact_info");
 $rows = array();


 while($row = $query->fetch(PDO::FETCH_ASSOC))
{
  $rows['contact_name'][]= $row;
  $rows['contact_email'][]= $row;
  $rows['contact_password'][]= $row;
  $rows['contact_trip'][]=$row;
}

 header("Content-type: application/json; charset=utf-8");

 echo json_encode($rows);

C#(片段):

    public class Customers : Android.Support.V4.App.Fragment
{
    List<people> mClient = new List<people>();
    ListView client;
    private WebClient webClient;
    private Uri mUrl;
    private BaseAdapter<people> mAdapter;

    public override void OnActivityCreated(Bundle savedInstanceState)
    {
        base.OnActivityCreated(savedInstanceState);
        client = View.FindViewById<ListView>(Resource.Id.list);

        webClient = new WebClient();
        mUrl = new Uri("http://*********.azurewebsites.net/admin.php");
        webClient.DownloadDataAsync(mUrl);
        webClient.DownloadDataCompleted += webClient_DownloadDataCompleted;

        MyListViewAdapter adapter = new MyListViewAdapter(this.Activity, mClient);
        client.Adapter = adapter;
    }

    private void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        Activity.RunOnUiThread(() =>
        {
            string json = Encoding.UTF8.GetString(e.Result);
            mClient = JsonConvert.DeserializeObject<List<people>>(json);
            mAdapter = new MyListViewAdapter(this.Activity, mClient);
            client.Adapter = mAdapter;
        });
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        var view = inflater.Inflate(Resource.Layout.Frag1Layout, container, false);
        return view;
       }


    public override string ToString()
    {
        return "Customers";
    }
}

C#(适配器):

   using System;
   using Android.App;
   using Android.Content;
   using Android.Widget;
   using Android.Runtime;
   using System.Collections.Generic;
   using Android.Views;
   using Android.OS;

   namespace Tonio_Pick_Me_Up.Droid
 {
class MyListViewAdapter : BaseAdapter<people>
{
    public List<people> mItems;
    private Context mContext;

    public MyListViewAdapter(Context context, List<people> items)
    {
        mItems = items;
        mContext = context;
    }

    public override int Count
    {
        get { return mItems.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override people this[int position]
    {
        get { return mItems[position]; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;

        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.contactlist, null, false);
        }

        TextView ContactName = row.FindViewById<TextView>(Resource.Id.ContactName);
        ContactName.Text = mItems[position].Contact_Name;

        TextView ContactEmail = row.FindViewById<TextView>(Resource.Id.ContactEmail);
        ContactName.Text = mItems[position].Contact_Email;

        TextView ContactNextTrip = row.FindViewById<TextView>(Resource.Id.ContactNextTrip);
        ContactName.Text = mItems[position].Contact_Email;

        return row;
    }
      }
   }


   C#(people class):

using System;
using Newtonsoft.Json;

namespace Tonio_Pick_Me_Up.Droid
{

public class people
{
    [JsonProperty("Contact_Name")]
    public string Contact_Name { get; set;}

    [JsonProperty("Contact_Email")]
    public string Contact_Email { get; set; }

    [JsonProperty("Contact_Trip")]
    public string Contact_Trip { get; set; }

}
}

希望你能提供帮助, 提前谢谢。

0 个答案:

没有答案