I'm new to xamarin and to json but I got to the point where I get a response from my php server with an internet browsers with the json code below , I know it is an object array and I made a class for the object using the option in visual studio option (Edit->Past Special->Past JSON as class) I only find how to do a login system but what I want to do is to see the data on a listview .Sorry for my bad English .
{
"error": false,
"users": [
{
"idCustomer": 38,
"firstname": "Dxxxx",
"surname": "Cxxxxx",
"gamertag": "Mxxxx",
"email": "dpxxxxx@xxxxxl.com",
"mobile": "012xxxxxx"
},
{
"idCustomer": 36,
"firstname": "Pxxxxx",
"surname": "Axxxxxx",
"gamertag": "Mxxxxxxx",
"email": "pxxxxxx@xxxxxxx.com",
"mobile": "0728468780"
},
{
"idCustomer": 37,
"firstname": "Dxxxxxx",
"surname": "Dxxxxxx",
"gamertag": "Axxxxxxxx",
"email": "txxxxxxx@xxxxxl.com",
"mobile": "011xxxxxxx"
},
{
"idCustomer": 39,
"firstname": "Exxxxxx",
"surname": "Axxxxxxx",
"gamertag": "Cxxxxxxx",
"email": "exxxxxx@xxxxxxxx.com",
"mobile": "012xxxxxxx"
},
{
"idCustomer": 40,
"firstname": "Cxxxxxx",
"surname": "Axxxxxxx",
"gamertag": "Bxxxxxxx",
"email": "cxxxxxxx.xxxxxxx@xxxxxxx.com",
"mobile": "012"
},
{
"idCustomer": 41,
"firstname": "Lenard",
"surname": "Neemoid",
"gamertag": "Gamers",
"email": "email@email.com",
"mobile": "10214785"
},
{
"idCustomer": 42,
"firstname": "Lenard",
"surname": "Nemoi",
"gamertag": "Spack",
"email": "email@email.com",
"mobile": "123456789"
},
{
"idCustomer": 43,
"firstname": "test",
"surname": "test",
"gamertag": "test",
"email": "test@test.com",
"mobile": "123456789"
},
{
"idCustomer": 44,
"firstname": "test",
"surname": "tees",
"gamertag": "tes",
"email": "ttess",
"mobile": "11222456"
},
{
"idCustomer": 51,
"firstname": "Dxxxxxxx",
"surname": "Lxxxxxxx",
"gamertag": "Sxxxxxxx",
"email": "Dxxxxxxx.Lxxxxxxx@Sxxxxxxx.com",
"mobile": "1234567890"
}
]
}
I have used this code but I do get an error
namespace LANApp
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
private ListView list_user;
private Button bnt_get_data;
private ArrayAdapter<string> adapter;
private SynchronizationContext sc;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
list_user = FindViewById<ListView>(Resource.Id.list_user);
bnt_get_data = FindViewById<Button>(Resource.Id.bnt_get_data);
adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line);
adapter.Add("No Data Yet");
list_user.Adapter = adapter;
sc = SynchronizationContext.Current;
bnt_get_data.Click += Bnt_get_data_Click;
}
private void Bnt_get_data_Click(object sender, EventArgs e)
{
getJSON();
}
private async void getJSON()
{
adapter.Clear();
IRestClient client = new RestClient("http://192.xxx.xxx.xxx/Laxxxxxx/public");
IRestRequest request = new RestRequest("allusers/", Method.GET);
try
{
await Task.Run(() =>
{
IRestResponse<List<User>> response = client.Execute<List<User>>(request);
foreach(var user in response.Data)
{
sc.Post(new SendOrPostCallback(o =>
{
adapter.Add(o as string);
adapter.NotifyDataSetChanged();
}), user.gamertag);
}
});
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
return;
}
}
}
and the error is Unhandled Exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference occurred
and my User class looks like this
namespace LANApp.Model
{
public class User
{
public int idCustomer { get; set; }
public string firstname { get; set; }
public string surname { get; set; }
public string gamertag { get; set; }
public string email { get; set; }
public string mobile { get; set; }
}
public class RootObject
{
public bool error { get; set; }
public List<User> users { get; set; }
}
}