Java 在else语句上打开新活动

Java 在else语句上打开新活动,java,android,Java,Android,我用android studio制作了简单的android登录应用程序。数据存储在在线服务器(MySQL数据库)中。注册和登录过程进展顺利。我想知道登录成功后如何打开“活动”。我尝试了这个代码,它给了我错误 protected void onPostExecute(String result) { if (result.equals("Registration Success")) { Toast.makeText(ctx,result,Toast.LENGTH_LO

我用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);





            }

        }


    }
包uc.venusha.com.loginsystem;
导入android.content.Context;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.support.v7.app.AlertDialog;
导入android.widget.Toast;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.OutputStream;
导入java.io.OutputStreamWriter;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.net.urlcoder;
/**
*由Venusha于2017年11月3日创建。
*/
公共类BackgroundTask扩展了AsyncTask{
警报对话框警报对话框;
上下文ctx;
背景任务(上下文ctx)
{
这个.ctx=ctx;
}
@凌驾
受保护的void onPreExecute(){
alertDialog=新建alertDialog.Builder(ctx.create();
alertDialog.setTitle(“登录信息”);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串ref_url=”http://ictguru.000webhostapp.com/webapp/register.php";
字符串登录\u url=”http://ictguru.000webhostapp.com/webapp/login.php";
字符串方法=参数[0];
if(方法等于(“寄存器”))
{
字符串名称=参数[1];
字符串user_name=params[2];
字符串user_pass=params[3];
试一试{
URL=新URL(参考URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
OutputStream OS=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(OS,“UTF-8”));
字符串数据=urlcoder.encode(“用户”、“UTF-8”)+“=”+urlcoder.encode(名称,“UTF-8”)+“&”+
URLEncoder.encode(“用户名”,“UTF-8”)+”=“+URLEncoder.encode(用户名,“UTF-8”)+”&”+
urlcoder.encode(“用户密码”,“UTF-8”)+“=”+urlcoder.encode(用户密码,“UTF-8”);
bufferedWriter.write(数据);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream=httpURLConnection.getInputStream();
IS.close();
返回“注册成功”;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
else if(method.equals(“login”)){
字符串login_name=params[1];
字符串login_pass=params[2];
试一试{
URL=新URL(登录\ URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OutputStream=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(outputStream,UTF-8));
字符串数据=urlcoder.encode(“登录名”,“UTF-8”)+“=”+urlcoder.encode(登录名,“UTF-8”)+“&”+
urlcoder.encode(“登录密码”,“UTF-8”)+“=”+urlcoder.encode(登录密码,“UTF-8”);
bufferedWriter.write(数据);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“iso-8859-1”);
字符串响应=”;
字符串行=”;
而((line=bufferedReader.readLine())!=null){
响应+=行;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
返回响应;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(void…值){
super.onProgressUpdate(值);
}
@凌驾
受保护的void onPostExecute(字符串结果){
如果(结果等于(“注册成功”))
{
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
否则{
//alertDialog.setMessage(结果);
//alertDialog.show();
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
意图=新意图(BackgroundTask.this,home.class);
主动性(意图);
}
}
}

尝试
Intent Intent=newintent(ctx,home.class)
和 尝试使用
ctx.startActivity(intent)
,而不是
startActivity(intent)

什么是