我正在使用android studio创建一个Android应用程序。登录页面使用PHP检查存储在MYSQL数据库中的详细信息的用户用户名和密码详细信息,并向用户提供有关输入的详细信息是成功还是不成功的信息。如果登录成功,我目前无法打开新活动。有人可以帮我这个吗?谢谢。
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.1.10/stafflogin.php";
String createAccount_url = "http://192.168.1.10/staffcreateaccount.php";
if(type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
答案 0 :(得分:1)
在onPostExecute
方法中添加此
if(result!=null && result.equals("successful")){
Intent intent = new Intent(LoginActivity.this,NextActivity.class);
startActivity(intent);
}
我编写了示例代码。请在“成功”,LoginActivity,NextActivity中使用正确的值。
答案 1 :(得分:0)
正如您所说,您正在收到消息...成功登录或未成功 然后你可以打开像...这样的新活动 在onPostExecute()..
if(result!=null && result.equalsIgnoreCase(message from PHP on succesful))
{
Intent intent = new Intent(context, NextPage.class);
startActivity(intent);
finish();
}
希望这会对你有帮助..
答案 2 :(得分:0)
假设您将获得成功响应代码200(状态正常)。这是你可以尝试的。
public class BackgroundWorker extends AsyncTask<String, Void, String> {
...
BackgroundWorker (Context ctx) {
context = ctx.getApplicationContext();
}
boolean statusOk = false;
@Override
protected String doInBackground(String... params) {
...
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
int statusCode = httpURLConnection.getResponseCode(); //get response code
if (statusCode == HttpURLConnection.HTTP_OK) {
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
statusOk = true;
return result;
}
}
@Override
protected void onPostExecute(String result) {
if (statusOk) {
Intent intent = new Intent(context, MainPage.class);// replace MainPage with the activity you want to start
context.startActivity(intent);
}
}
}
答案 3 :(得分:0)
由于您在运行php
文件后获得了结果,因此您可以使用onPostExecute()
方法启动新的activity
。如果您愿意,可以使用以下data
activity
传递给将在此处打开的新code
@Override
protected void onPostExecute(String result) {
if(result!=null && results.equals("what ever you receive from the php"){
Intent i= new Intent(this,theNameOfTheActivityToBeOpen.class);
i.putExtra("nameYouWantToReferInOtherActivity",result);
startActivity(i);
}
else{
}
alertDialog.setMessage(result);
alertDialog.show();
并接收发送到新活动的内容使用以下代码
Bundle anyNameForYourBundle=getIntent().getExtras();
//since a String is passed
String temp = anyNameForYourBundle.getString("nameYouWantToReferInOtherActivity");
答案 4 :(得分:0)
试试这是工作
public class BackgroundWorker extends AsyncTask<String, Void, String> {
...
BackgroundWorker (Context ctx) {
context = ctx.getApplicationContext();
}
boolean statusOk = false;
@Override
protected String doInBackground(String... params) {
...
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
int statusCode = httpURLConnection.getResponseCode(); //get response code
if (statusCode == HttpURLConnection.HTTP_OK) {
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
statusOk = true;
return result;
}
}
@Override
protected void onPostExecute(String result) {
if (statusOk) {
Intent intent = new Intent(context, MainPage.class);// replace MainPage with the activity you want to start
context.startActivity(intent);
}
}
}