2012-06-18 59 views
0

我正在開發一個小應用程序,並且需要進行身份驗證。我試圖通過php連接到MYsql db來開發。Android通過php遠程連接Mysql數據庫 - 登錄身份驗證

添加uname/pw - > loging - >發送到php - > check db - > ok發送回到android - > authenticated。 /錯誤發回重新登錄。

我已經開發到目前爲止,我認爲這是應該如何,但我不確定,因爲我真的是新的cording。
請善待我的幫助。謝謝。

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()); 
        } 
      } 
     }); 
    } 
} 
+0

什麼是你真正的錯誤?它是否成功登錄...或不是? –

+0

它沒有給出任何錯誤......我創建了這個以及谷歌幫助,我不知道如何編寫php代碼以及連接到Mysql數據庫 –

回答

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); 
      } 
     }); 




    } 

}