0

私は小さなアプリケーションを開発しており、そのためには認証が必要です。PHP経由でMYsql dbに接続して開発しようとしています。

uname/pw を追加 --> ロギング --> php に送信 --> データベースをチェック --> 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 に答える