2015-09-07 57 views
5

我想在登录成功后开始一项新活动。那就是当登录进行正确时,一个新的活动应该自动启动......我不知道在哪里提到startactivity。请帮助我,这是一个新手android。 这是我的代码...登录成功后如何开始新的活动?

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 reg_url = ""; 
     String login_url = ""; 
     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); 

       //httpURLConnection.setDoInput(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(); 
       //httpURLConnection.connect(); 
       httpURLConnection.disconnect(); 
       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(); 
     } 

    } 
} 
+2

对不请自来的建议:如果doInBackground有两个以上的可能结果,我建议改变onPostExecute以获得一个Integer参数,这样你就可以对结果值做一个开关事件:例如“Unknown username,connection timeout “等 – user5292387

+1

此外,在您发布的代码中放置实时网址或其他潜在敏感或专有信息不是一个好主意。可能想清理这些URL。 – user5292387

回答

2

你可以把意图在onPostExecute方法是这样的:

@Override 
    protected void onPostExecute(String result) { 
     if(result == null) 
     { 
      // do what you want to do 
     } 
     else if(result.equals("Registration Success...")) 
     { 
      Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
     } 
     else if(result.contains("Login Success")) // msg you get from success like "Login Success" 
     { 
      Intent i = new Intent(ctx,NewActivity.class); 
      ctx.startActivity(i); 
      alertDialog.setMessage(result); 
      alertDialog.show(); 
     } 

    } 

编辑: 如果你想调用登录​​成功后新的活动,然后把意图在其他部分。

+0

感谢您的回复。但如果我把意图放在这里,那么它会在注册成功后执行......对吗? 但我想登录成功后 –

+0

@rijoraju检查我编辑的答案。 – SANAT

+0

@rijoraju它可以帮助你吗? – SANAT

0

在postExecute()方法中,您必须检查方法变量,请检查以下代码。

public class BackgroundTask extends AsyncTask<String,Void,String> { 
AlertDialog alertDialog; 
Context ctx; 
String method; 
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 reg_url = "http://thecapitalcitychurch.16mb.com/new/register.php"; 
    String login_url = "http://thecapitalcitychurch.16mb.com/new/login.php"; 
    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); 

      //httpURLConnection.setDoInput(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(); 
      //httpURLConnection.connect(); 
      httpURLConnection.disconnect(); 
      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(); 
    } 

    if(method.equals("login") && result.equals("Login Success...")) 
    { 
     Intent intent=new Intent(ctx, Dashboard.class); 
     ctx.startActivity(intent); 
    } 
} 
} 
+0

它在method.equals中给出错误 –

+0

您必须在BackgroundTask中声明方法变量global类。 –

+0

对不起,哥们我仍然收到错误 你能否只是声明它请求其请求 –

0

呼叫您的onPostexecute

protected void onPostExecute(String response) { 
     try {Intent intent = new Intent(context, MainActivity.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(intent); 
}catch(Exception e){}} 
1

试试这个

Intent intent = new Intent(MainActivity.this, Second.class); 
startActivity(intent); 
0

至于要立即开始另一个活动登录呼叫被成功那么你可以开始其他活动无论是从呼叫返回登录响应或从AsyncTask的onpostExecute方法。

为了在API流程中实现回调,您只需要创建一个没有主体(仅声明)的方法很少的接口,并且这些方法必须在实现该接口的类中实现。将此接口对象作为asyncTask的构造函数传递。一旦登录调用成功,您需要调用该接口的方法,并在该方法内部启动另一个活动。