我用android studio制作了简单的android登录应用程序。数据存储在在线服务器(MySQL数据库)中。注册和登录过程表现良好。我想知道如何在登录成功时打开活动。我试过这个代码,它给了我错误。
protected void onPostExecute(String result) {
if (result.equals("Registration Success"))
{
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
else {
// alertDialog.setMessage(result);
// alertDialog.show();
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
Intent intent = new Intent(BackgroundTask.this,home.class);
StartAcitivity(intent);
这是后台任务类的完整代码
package uc.venusha.com.loginsystem;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by Venusha on 11/3/2017.
*/
public class BackgroundTask extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx=ctx;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information");
}
@Override
protected String doInBackground(String... params) {
String ref_url= "http://ictguru.000webhostapp.com/webapp/register.php";
String login_url="http://ictguru.000webhostapp.com/webapp/login.php";
String method = params[0];
if (method.equals("register"))
{
String name = params[1];
String user_name=params[2];
String user_pass=params[3];
try {
URL url= new URL(ref_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("user","UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
URLEncoder.encode("user_name","UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
URLEncoder.encode("user_pass","UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS =httpURLConnection.getInputStream();
IS.close();
return "Registration Success";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("login")){
String login_name=params[1];
String login_pass=params[2];
try {
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 data = URLEncoder.encode("login_name","UTF-8") +"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response ="";
String line ="";
while ((line=bufferedReader.readLine())!=null) {
response+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values){
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
if (result.equals("Registration Success"))
{
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
else {
// alertDialog.setMessage(result);
// alertDialog.show();
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
Intent intent = new Intent(BackgroundTask.this,home.class);
StartAcitivity(intent);
}
}
}
答案 0 :(得分:0)
尝试Intent intent=new Intent(ctx,home.class)
和
而不是StartAcitvity(intent)
,请尝试使用ctx.startActivity(intent)