成功登录后如何进入下一个活动?

时间:2012-01-23 18:15:09

标签: android

问题: 我已成功将我的应用程序连接到我的服务器,并且我得到了“成功登录”...问题是,在该连接之后,我无法在成功登录后将我的活动传递给Action。这是我的代码

public class AndroidLogin extends Activity implements OnClickListener {


    Button ok,back,exit;
    TextView result;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Login button clicked
        ok = (Button)findViewById(R.id.btn_login);
        ok.setOnClickListener(this);

        result = (TextView)findViewById(R.id.lbl_result);

    }

    public void postLoginData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();


        HttpPost httppost = new HttpPost("http://www.xxxx.com/login.php");

        try {
            // Add user name and password
            EditText uname = (EditText)findViewById(R.id.txt_username);
            String username = uname.getText().toString();

            EditText pword = (EditText)findViewById(R.id.txt_password);
            String password = pword.getText().toString();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            Log.w("KASIRIS", "Execute HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);

            String str = inputStreamToString(response.getEntity().getContent()).toString();
            Log.w("KASIRIS", str);

            if(str.toString().equalsIgnoreCase("true"))
            //  startActivity(new Intent("com.sencide.OK"));
            {
                Log.w("KASIRIS", "TRUE");
                result.setText("Login successful");   
                Intent intent=new Intent(getApplicationContext(),Ok.class);
                startActivity(intent);
                finish();


            }else
            {
                Log.w("KASIRIS", "FALSE");
            //  finish();
                result.setText(str);  

            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

    private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) { 
                total.append(line); 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Return full string
        return total;
    }

    public void onClick(View view) {
        if(view == ok){
            postLoginData();

        }
           //start new Activity
    }

}

0 个答案:

没有答案