android - 如何在登录成功后开始另一个 Activity

标签 android

我如何在登录 Activity 成功后开始 Activity 。我试过这个,how to start an activity after my login Activity但似乎对我不起作用。我已经把我的登录 Activity 放在我的 main.. 然后我的警告消息在一个 php 脚本中.. 这是我的 backgroundtask 编码:

@Override
protected String doInBackground(String... params) {
    String reg_url = "http://10.0.2.2/webapp/register.php";
    String login_url = "http://10.0.2.2/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(reg_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 onPostExecute(String result) {
    if(result.equals("Registration Success..."))
    {
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
    }
    else if(result.contains("Login Success")) 
    {
        Intent i = new Intent(getApplicationContext,NewActivity.class);
        getApplicationContext.startActivity(i);
        alertDialog.setMessage(result);
        alertDialog.show();
    }


 }
}

请帮我解决这个问题......我已经花了一个星期试图解决这个问题.. tq

最佳答案

在登录过程中,您尝试调试登录成功时'response'(第66行)的值

这是调试“响应”的两种方法

  1. 使用带断点的调试工具
  2. 在返回响应之前,放入下面的代码并观察您的 Logcat。

    Log.e("CODE", "response : "+ response);

当您知道响应的内容时,将您的 onPostExecute 更改为以下代码,并将 {$CONTENTS YOU DEBUGGED} 替换为调试结果。

@Override
protected void onPostExecute(String result) {
    if (result.equals("Registration Success...")) {
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
    } else if(result.contains("Hello") { 
        Intent i = new Intent(ctx, SomethingClass.class);
        ctx.startActivity(i);
    } else {
        alertDialog.setMessage(result);
        alertDialog.show();
    }
}

编辑))

响应模式是 Hello Welcome + $user。因此,当响应包含“Hello”时,它将移动另一个 Activity。就是这样。

关于android - 如何在登录成功后开始另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289771/

相关文章:

android - 在存储库容器上找不到参数 [] 的方法 google()。 ( flutter )

Android 膨胀自定义 View 的正确位置

php - 我如何实现在我的应用程序上选择图像并将其发送到远程数据库(JSON+PHP)的可能性?

android - Jetpack 撰写 ui : How to create cardview?

android - 如何从不受支持的国家(拉脱维亚)向 Android 市场添加非免费应用程序?

android - 如何删除 Android 4+ 中的设置向导应用程序?

java - fragment 中按钮的 onClickListener 问题

java - 如何随机化 RadioButton?

java - Fragments 作为静态内部类与独立公共(public)类的设计逻辑是什么?

java - 从文件夹加载图像并将其绘制到 android 中