这是代码,我想要做的就是测试是否在数据库中输入了用户凭据,然后通过 toast 消息显示成功,如果它们是,并且他们不返回错误消息
以下是我从其中一个网站上发现的所有说明,最终得到了以下代码,但吐司仍然没有显示出来。
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views.InputMethods;
using Android.Content;
using Android.Views;
using System;
using System.Net;
using System.Collections.Specialized;
using Org.Json;
using System.Text;
namespace App
{
[Activity(Label = "App", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity, Android.Views.View.IOnClickListener
{
EditText username, password;
Button signIn;
signInAsync sn;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
initialize();
}
public void initialize()
{
username = (EditText)FindViewById(Resource.Id.editText1);
password = (EditText)FindViewById(Resource.Id.editText2);
signIn = (Button)FindViewById(Resource.Id.button1);
signIn.SetOnClickListener(this);
}
public override bool OnTouchEvent(MotionEvent e)
{
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
EditText username = (EditText)FindViewById(Resource.Id.editText1);
EditText password = (EditText)FindViewById(Resource.Id.editText2);
//Cisti fokus
username.ClearFocus();
password.ClearFocus();
//imm.HideSoftInputFromWindow(username.WindowToken, 0);
return base.OnTouchEvent(e);
//Sklanja tastaturu s ekrana na klik na pozadinu.
}
public void OnClick(View v)
{
switch (v.Id)
{
case Resource.Id.button1:
sn = new signInAsync(this);
sn.Execute();
break;
}
}
public class signInAsync : AsyncTask
{
MainActivity mainActivity;
public signInAsync(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
string username, password;
protected override void OnPreExecute()
{
base.OnPreExecute();
username = mainActivity.username.Text;
password = mainActivity.password.Text;
}
protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
{
WebClient client = new WebClient();
client.UseDefaultCredentials = true;
client.Proxy.Credentials = CredentialCache.DefaultCredentials;
Uri uri = new Uri("http://192.168.1.198/android/login.php");
NameValueCollection parameters = new NameValueCollection();
parameters.Add("username", username);
parameters.Add("password", password);
var response = client.UploadValues(uri, parameters);
var responseString = Encoding.UTF8.GetString(response);
JSONObject ob = new JSONObject(responseString);
if (ob.OptString("success").Equals("1"))
{
mainActivity.RunOnUiThread(() =>
{
Toast.MakeText(mainActivity, "Uspješno ste se ulogovali", ToastLength.Short).Show();
});
};
if (ob.OptString("error").Equals("2"))
Toast.MakeText(mainActivity, "Pogresno", ToastLength.Short).Show();
if (ob.OptString("error").Equals("3"))
Toast.MakeText(mainActivity, "Error", ToastLength.Short).Show();
return null;
}
}
}
}
这是我的PHP文件:
<?php
$db_name = "korisnici";
$mysql_username = "Mpro";
$mysql_password = "prolinet";
$server_name = "192.168.1.198";
$conn = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);
if($conn) {
echo "Connection success";
} else {
echo "Faliure to connect";
}
if(isset($_POST['username']) && isset($_POST['password'])) {
$user_name = $_POST['username'];
$user_pass = $_POST['password'];
$mysql_qry = "SELECT * FROM korisnici WHERE username = '$user_name' AND password = '$user_pass'";
if(mysql_fetch_row($mysql_qry)){
$response["success"] = 1;
echo json_encode($response);
} else{
$response["error"]=2;
echo json_encode($response);
}
} else {
$response["error"] = 3;
echo json_encode($response);
}
?>
很抱歉打扰你,但我是Xamarin初学者。
答案 0 :(得分:0)
更改此行
Toast.MakeText(mainActivity, "Uspješno ste se ulogovali", ToastLength.Short).Show();
使用:
Toast.MakeText(mainActivity.this, "Uspješno ste se ulogovali", ToastLength.Short).Show();
希望有所帮助
答案 1 :(得分:0)
如果你想在asntectask的onPostExcute()方法中使用asyntask成功或失败时显示toast成功或失败。
答案 2 :(得分:0)
首先确保执行显示吐司的行。 您可以使用Log类打印一些消息,以确保应用程序中发生的事情。
答案 3 :(得分:0)
使用getContext
或mainActivity.this
代替mainActivity
答案 4 :(得分:0)
您无法确定您的活动是否在后台线程中处于有效状态。因此,而不是在应用程序上下文中传递活动传递,如下所示:
public signInAsync(Context appContext)
然后做:
Toast.MakeText(appContext,...
因此,当您从某个活动中调用signInAsync时,您会将其称为:
signInAsync(this.ApplicationContext)