0

我正在开发一个小型应用程序,并且需要进行身份验证。我正在尝试通过 php 连接到 MYsql db 来进行开发。

添加 uname/pw --> 日志记录 --> 发送到 php --> 检查 db --> ok 发送回 android --> 已验证。/ 错误发送回重新登录。

到目前为止,我已经发展了,我认为应该是这样,但我不确定,因为我对绳索真的很陌生。
请善意地帮助我进行接线。谢谢你。

public class Loging extends Activity{

    TextView txt;
    EditText uname,pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.loging);
        Button b = (Button)findViewById(R.id.log);
        uname = (EditText)findViewById(R.id.uname);
        pwd = (EditText)findViewById(R.id.pwd);

        b.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                    nameValuePairs.add(new BasicNameValuePair("name", uname.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("Pass", pwd.getText().toString()));

                    try 
                    {
                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost("http://www.helloworld.com/report.php");
                     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     HttpResponse response = httpclient.execute(httppost);
                     HttpEntity entity = response.getEntity();
                     InputStream is = entity.getContent();
                    }
                    catch(Exception e)
                    {
                            Log.e("log_tag","Error in http connection"+e.toString());
                    }   
            }
        });
    }
}
4

1 回答 1

0
public class Loging extends Activity{

    TextView txt;
    EditText uname,pwd;
    final Loging login = this; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.loging);
        Button loging = (Button)findViewById(R.id.log);
        Button adduser = (Button)findViewById(R.id.adduser);
        uname = (EditText)findViewById(R.id.uname);
        pwd = (EditText)findViewById(R.id.pwd);


        loging.setOnClickListener(new View.OnClickListener() {


            public void onClick(View arg0) {
                // TODO Auto-generated method stub


                // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();

                /* login.php returns true if username and password is equal to saranga */
                HttpPost httppost = new HttpPost("http://www.xoxoxoxox.com/login.php");

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

                 EditText pword = (EditText)findViewById(R.id.pwd);
                 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("SENCIDE", "Execute HTTP Post Request");
                    HttpResponse response = httpclient.execute(httppost);

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

                    if(str.toString().equalsIgnoreCase("true"))
                    {
                     Log.w("SENCIDE", "TRUE");
                     //result.setText("Login successful");   

                     Intent in = new Intent();
                        in.setClass(login, MappingActivity.class);
                                startActivity(in);
                    }else
                    {
                     Log.w("SENCIDE", "FALSE");
                     AlertDialog alertDialog = new AlertDialog.Builder(Loging.this).create();
                        alertDialog.setTitle("Credentials Missing");
                        alertDialog.setMessage("Please add UserName and Password");
                        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int which) {
                              // here you can add functions
                               Intent i = new Intent();
                                i.setClass(login, MappingActivity.class);
                                        startActivity(i);
                           }
                        });
                        alertDialog.show();            
                    }

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


            }

            private Object inputStreamToString(InputStream is) {
                // TODO Auto-generated method stub
                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;
            }
        });

        adduser.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in = new Intent();
                in.setClass(login, Adduser.class);
                        startActivity(in);
            }
        });




    }

}
于 2012-06-20T05:36:18.240 回答