i don't know why my async not waiting the process, here is my code :
userKu = new UserData();
btnLogin = FindViewById<Button>(Resource.Id.buttonLogin);
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
if (isOnline() == true)
{
AppPreferences ap = new AppPreferences(mContext);
string usernameCekLogin = ap.getNamaUser();
//TOAST #1
Toast.MakeText(this, userKu.status, ToastLength.Long).Show();
isLogin(usernameCekLogin, userKu);
//TOAST #2
Toast.MakeText(this, userKu.status, ToastLength.Long).Show();
btnLogin.Click += delegate{}
}
}
private async Task<string> FetchUserAsync(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 (var sr = new StreamReader(response.GetResponseStream()))
{
string strContent = sr.ReadToEnd();
return strContent;
}
}
}
private async void isLogin(string username, UserData data1)
{
string url = "http://localhost.com/api.php/isLogin/login_mbr/"+username;
data1 = JsonConvert.DeserializeObject<UserData>(await FetchUserAsync(url));
//TOAST 3
Toast.MakeText(this, "check user status = "+data1.status, ToastLength.Long).Show();
}
When I run it, I don't get the value on #TOAST 1 and #TOAST 2, but on #TOAST 3 I get the value "0" or "1".
If I change protected override async void OnCreate(Bundle bundle)
and I don't use function like this :
userKu = new UserData();
btnLogin = FindViewById<Button>(Resource.Id.buttonLogin);
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
if (isOnline() == true)
{
string url = "http://localhost.com/api.php/isLogin/login_mbr/";
AppPreferences ap = new AppPreferences(mContext);
string usernameCekLogin = ap.getNamaUser();
url += usernameCekLogin;
//TOAST 1
Toast.MakeText(this, userKu.status, ToastLength.Long).Show();
userKu = JsonConvert.DeserializeObject<UserData>(await FetchUserAsync(url));
//TOAST 2
Toast.MakeText(this, userKu.status, ToastLength.Long).Show();
/*
btnLogin.Click += delegate{}*/
}
}
private async Task<string> FetchUserAsync(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 (var sr = new StreamReader(response.GetResponseStream()))
{
string strContent = sr.ReadToEnd();
return strContent;
}
}
}
On #Toast 1 I don't get value and on #Toast 2 I get the value. But I get error when I enable my button, it said null references. Any Solution for my problems? I'm using Visual Studio 2015 and Xamarin for developing android app.
Thanks in advance