Java 如果在Android中注册成功,我如何进入登录活动?

Java 如果在Android中注册成功,我如何进入登录活动?,java,android,login,android-asynctask,Java,Android,Login,Android Asynctask,服务器端的一切都正常工作。我的问题是,如果HTTP响应为“注册成功”,我不确定如何返回登录页面 我的Login和Register类都依赖于BackgroundTask来处理异步操作 下面是BackgroundTask的代码 public class BackgroundTask extends AsyncTask<String, Void, String> { AlertDialog mAlertDialog; Context context; private

服务器端的一切都正常工作。我的问题是,如果HTTP响应为“注册成功”,我不确定如何返回登录页面

我的Login和Register类都依赖于BackgroundTask来处理异步操作

下面是BackgroundTask的代码

public class BackgroundTask extends AsyncTask<String, Void, String> {
    AlertDialog mAlertDialog;
    Context context;
    private CheckTask mCheckTask;


    BackgroundTask(Context context, Boolean login, Boolean register) {
        this.context = context;
        mCheckTask = new CheckTask(login, register);
    }

    @Override
    protected String doInBackground(String... params) {

        String reg_url = "http://www.myegotest.com/register.php";
        String login_url = "http://www.myegotest.com/login.php";

        ////////////////////////REGISTER SCRIPT/////////////////////////////
        if (mCheckTask.getIsRegisterTask()) {
            String first_name = params[0];
            String last_name = params[1];
            String username = params[2];
            String password = params[3];
            try {
                //Set the URL we are working with
                URL url = new URL(reg_url);

                //Open a url connection and set params for the url connection
                HttpURLConnection LucasHttpURLConnection = (HttpURLConnection) url.openConnection();
                LucasHttpURLConnection.setRequestMethod("POST");
                LucasHttpURLConnection.setDoOutput(true);
                LucasHttpURLConnection.setDoInput(true);
                //Retrieve the output stream
                OutputStream os = LucasHttpURLConnection.getOutputStream();
                //Create a buffered writer to write the data to the output stream output stream.
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                //Encode Data we are sending on the output stream
                String data = URLEncoder.encode("first_name", "UTF-8") + "=" + URLEncoder.encode(first_name, "UTF-8") + "&" +
                                      URLEncoder.encode("last_name", "UTF-8") + "=" + URLEncoder.encode(last_name, "UTF-8") + "&" +
                                      URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
                                      URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                //Write the data to the output stream, and close buffered writer
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                //Close output stream
                os.close();
                //InputStream to get response
                InputStream IS = LucasHttpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1"));
                String response = "";
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    response += line;
                }
                bufferedReader.close();
                IS.close();
                //LucasHttpURLConnection.disconnect();
                return response;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //////////////////////////////////LOGIN SCRIPT/////////////////////////////////////////
        } else if (mCheckTask.getIsLoginTask()) {
            String username = params[0];
            String password = params[1];
            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("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
                                      URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                //InputStream to get response
                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1"));
                String response = "";
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    response += line;
                }
                bufferedReader.close();
                IS.close();
                httpURLConnection.disconnect();
                return response;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        if (mCheckTask.getIsLoginTask()) {
            mAlertDialog = new AlertDialog.Builder(context).create();
            mAlertDialog.setTitle("Login Information... ");
        } else {
            mAlertDialog = new AlertDialog.Builder(context).create();
            mAlertDialog.setTitle("Register Information... ");
        }
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        if (result.equals("Please choose another username")) {
            mAlertDialog.setMessage(result);
            mAlertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            });
            mAlertDialog.show();

        } else if (result.equals("Registration success!")) {
            mAlertDialog.setMessage(result);
            mAlertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            mAlertDialog.show();
        } else {
            mAlertDialog.setMessage(result);
            mAlertDialog.show();
        }
    }
}
公共类BackgroundTask扩展了AsyncTask{
AlertDialog-mAlertDialog;
语境;
私有检查任务mCheckTask;
背景任务(上下文、布尔登录、布尔寄存器){
this.context=上下文;
mCheckTask=新的检查任务(登录、注册);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串注册表url=”http://www.myegotest.com/register.php";
字符串登录\u url=”http://www.myegotest.com/login.php";
////////////////////////注册脚本/////////////////////////////
if(mCheckTask.getIsRegisterTask()){
字符串first_name=params[0];
字符串last_name=params[1];
字符串username=params[2];
字符串密码=参数[3];
试一试{
//设置我们正在使用的URL
URL=新URL(注册URL);
//打开url连接并设置url连接的参数
HttpURLConnection lucashttppurlconnection=(HttpURLConnection)url.openConnection();
LucasHttpURLConnection.setRequestMethod(“POST”);
LucasHttpURLConnection.setDoOutput(true);
LucasHttpURLConnection.setDoInput(true);
//检索输出流
OutputStream os=LucasHttpURLConnection.getOutputStream();
//创建一个缓冲写入器,将数据写入输出流。
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(os,“UTF-8”));
//对我们在输出流上发送的数据进行编码
字符串数据=urlcoder.encode(“名字”,“UTF-8”)+”=“+urlcoder.encode(名字,“UTF-8”)+”&”+
URLEncoder.encode(“姓氏”,“UTF-8”)+”=“+URLEncoder.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以获取响应
InputStream IS=LucasHttpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(IS,“iso-8859-1”);
字符串响应=”;
弦线;
而((line=bufferedReader.readLine())!=null){
响应+=行;
}
bufferedReader.close();
IS.close();
//LucasHttpURLConnection.disconnect();
返回响应;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
//////////////////////////////////登录脚本/////////////////////////////////////////
}else if(mCheckTask.getIsLoginTask()){
字符串username=params[0];
字符串密码=参数[1];
试一试{
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));
字符串数据=URLEncoder.encode(“用户名”,“UTF-8”)+”=“+URLEncoder.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(IS,“iso-8859-1”);
字符串响应=”;
弦线;
而((line=bufferedReader.readLine())!=null){
响应+=行;
}
bufferedReader.close();
IS.close();
httpURLConnection.disconnect();
返回响应;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onPreExecute(){
if(mCheckTask.getIsLoginTask()){
mAlertDialog=新建AlertDialog.Builder(context.create();
setTitle(“登录信息…”);
}否则{
mAlertDialog=新建AlertDialog.Builder(context.create();
setTitle(“注册信息…”);
}
}
@凌驾
受保护的void onProgressUpdate(void…值){
Public class Dispatch extends ActionBarActivity {

  @Override
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    // Check if there is current user info
    if (ParseUser.getCurrentUser() != null) {
        // Start an intent for the logged in activity
        startActivity(new Intent(this, Home.class));
    } else {
        // Start and intent for the logged out activity
        startActivity(new Intent(this, login.class));
    }
}

}