且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

登录后如何启动活动

更新时间:2023-12-04 11:52:52

在此处使用Aysntask

doInBackground()中执行哪个运算,并在onPostExecute()中给出结果

Which do operation in doInBackground() result and give result in onPostExecute()

public class Login_Menu extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_lay);
    final TextView tv=(TextView) findViewById(R.id.login_stat_tv);
    final EditText uname=(EditText)findViewById(R.id.uname);
    final EditText pass=(EditText)findViewById(R.id.pass);
    Button login=(Button)findViewById(R.id.login_but);
    Button cancel=(Button)findViewById(R.id.cancel_but);

    final HttpClient client = new DefaultHttpClient();
    String url="http://10.0.2.2:7001/f/json.jsp";
    final HttpPost post = new HttpPost(url);
    new login().execute("");
 }


private class login extends AsyncTask<String, Void, Void>{
    ProgressDialog dialog = ProgressDialog.show(activity.this, "", "Loading, Please wait...");

    @Override
    protected int doInBackground(String... params) {
        // TODO Auto-generated method stub
        Log.i("thread", "Doing Something...");
       //authentication operation
try{

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();   
        pairs.add(new BasicNameValuePair("username",uname.getText().toString()));   
        pairs.add(new BasicNameValuePair("password",pass.getText().toString()));   
        post.setEntity(new UrlEncodedFormEntity(pairs));   
        HttpResponse response = client.execute(post);

        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        String line = null;

        while ((line = rd.readLine()) != null) {

            tv.append(line);

        }
        //startActivity(new Intent("com.campuspro.start.DEMO_RETRV"));
      }

    catch(Exception e)
    {
        e.printStackTrace();   

    }

        return val;
    }

    protected void onPreExecute(){
        //dialog.dismiss();
        Log.i("thread", "Started...");
        dialog.show();
    }
    protected void onPostExecute(int result){
        Log.i("thread", "Done...");
        if(dialog!=null)
        dialog.dismiss();
        if(result){
            toast.setText("No User Found, please try again!");
            toast.show();
        }else{
        Intent myIntent = new Intent(ctx, main.class);
        myIntent.putExtra("user", user);
        startActivity(myIntent);
        }
    }

}