想要在Android应用程序中购买书籍时验证用户

时间:2014-09-01 18:01:47

标签: java android http

我用eclipse在android上做了一个书店。我有一个主页面如果用户点击登录图标,请转到登录并输入用户名和密码,然后按登录按钮, 如果用户发现“用户找到”的节目按摩,如果用户未找到显示警告消息“未找到”
我想如果用户登录并看到消息“用户找到” 当用户想购买书籍时可以购买 而且什么时候登录不能买书

这意味着当转到用户页面时(用户页面用于购买书籍) 用户登录时,此页面显示 并且当不登录此页面时转到登录活动 , 我用旗帜但我不知道如何使用它?如果flag = 0,则用户不登录,如果flag = 1用户登录。但是如何将其转移到用户页面?

可以帮帮我吗? 是否有任何网站?

loginactivity.java

public class LoginActivity extends Myactionbar {
EditText userNmae,pass;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
Button btnLinkToRegister;
Button btnLogin;
int flag=0;

public void gotosearch() {
    Intent intent = new Intent(this, Search.class);
    startActivity(intent);}
    public void gotologin() {
        Intent m = new Intent(this, LoginActivity.class);
        startActivity(m);}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.login);

    userNmae=(EditText)findViewById(R.id.usernameedittext);
    pass=(EditText)findViewById(R.id.passwordedittext);


    Typeface font1 = Typeface.createFromAsset(getAssets(),
            "font/bnazanin.TTF");

    //emailtext
    TextView userName = (TextView) findViewById(R.id.usernametv);
    ///tv.setText(booksote.utils.DariGlyphUtils.reshapeText("email"));
    userName.setTypeface(font1);
    userName.setTextSize(26);


    //passwordtext
    TextView password1 = (TextView) findViewById(R.id.passwordtv);  
    //tv.setText(booksote.utils.DariGlyphUtils.reshapeText("password"));
    password1.setTypeface(font1);
    password1.setTextSize(26);



    //login
    btnLogin= (Button) findViewById(R.id.btnLogin);

    btnLogin.setTypeface(font1);
    btnLogin.setTextSize(26);
     **btnLogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(LoginActivity.this, "", 
                        "Validating user...", true);
                 new Thread(new Runnable() {
                        public void run() {
                            login();                      
                        }
                      }).start();               
            }
        });

}

void login(){
    try{            

        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://10.0.2.2/project/login1.php");
        //add your data
        nameValuePairs = new ArrayList<NameValuePair>(2);


        nameValuePairs.add(new BasicNameValuePair("user_name",userNmae.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
        nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
        response=httpclient.execute(httppost);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response :" + response); 
        runOnUiThread(new Runnable() {
            public void run() {
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("User Found")){
            runOnUiThread(new Runnable() {
                public void run() {
                    //Toast.makeText(LoginActivity.this,"Login Success", Toast.LENGTH_SHORT).show();
//this I use  flag but I dontknow how to use ?

                    flag=1;


            **Intent intent = new Intent(getApplicationContext(),UserPage.class);
            intent.putExtra("flag", flag);**

            startActivity(intent);**




                }

            });


        }else{
            flag=0;
            showAlert();                
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }




}


public void showAlert(){
    LoginActivity.this.runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
            builder.setTitle("Login Error.");
            builder.setMessage("not found")  
                   .setCancelable(false)
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                       }
                   });                     
            AlertDialog alert = builder.create();
            alert.show();               
        }
    });
}

}

userpage.java

public class UserPage extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_page);

    Intent intent = getIntent();

    int myflag = intent.getIntExtra("flag");


}

0 个答案:

没有答案