2012-11-15 43 views
0

我是新手android應用程序。我做了一個登錄頁面,只有已經從網站註冊的用戶才能登錄。當用戶輸入他的用戶名和密碼時,我想驗證註冊用戶。我如何驗證用戶名和密碼。我正在使用下面的代碼,請看看這個並幫助我。當點擊登錄按鈕時,會顯示一些錯誤。登錄認證只爲註冊用戶從網站

Main.java

public class Main extends Activity implements OnClickListener { 

     Button ok,back,exit; 
     TextView result; 



    @Override 
     public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_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(); 

    /* login.php returns true if username and password is equal to saranga */ 
    HttpPost httppost = new HttpPost("http://yoursite.com/wp-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("", "Execute HTTP Post Request"); 
     HttpResponse response = httpclient.execute(httppost); 

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

     if(str.toString().equalsIgnoreCase("true")) 
     { 
     Log.w("", "TRUE"); 
     result.setText("Login successful"); 
     }else 
     { 
     Log.w("", "FALSE"); 
     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(); 
       } 
       } 

       } 

錯誤

Execute HTTP Post Request 
    Default buffer size used in BufferedReader constructor. It would be better to be  explicit if an 8k-char buffer is required. 
    <html><head> <title>yoursite.com: The Leading Website Site on the Net</title>   <script type="text/javascript">;  if(self != top) top.location.href = 'http://'+location.hostname+'/?redir=frame& uid=yoursite50a47fe3380989.09080642';  </script>  <script type="text/javascript" src="http://return.bs.domainnamesales.com /return_js.php?d=yoursite.com&s=1352957923"></script> </head> <frameset cols="1,*,1" border=0>  <frame name="top" src="tg.php?uid=yoursite50a47fe3380989.09080642" scrolling=no frameborder=0 noresize framespacing=0 marginwidth=0 marginheight=0>  <frame src="search.php?uid=yoursite50a47fe3380989.09080642" scrolling="auto" framespacing=0 marginwidth=0 marginheight=0 noresize>  <frame src="page.php?yoursite50a47fe3380989.09080642"></frame> </frameset>  <noframes>  yoursite.com has been connecting our visitors with providers of Computer Networking, Dedicated Web Servers, Email Domain Hosting and many other related services for nearly 10 years. Join thousands of satisfied visitors who found Ftp Hosts, Ftp Servers, Host, Internet Servers, and Linux Servers.<br/> </noframes></html> 
FALSE 

回答

1

嘗試這個例子。

http post method passing null values to the server

在這裏,可以發送用戶名&密碼到Web服務器,你會得到來自服務器的響應按該請求。所以如果使用註冊或不註冊,您必須在服務方檢查。我使用JSON檢索響應。

+0

JSONObject json_data = new JSONObject(result); //其包含輸出的字符串var。 my_output_one = json_data.getString(「var_1」); //它的響應var形式爲web。 my_output_two = json_data.getString(「var_2」); – SRY

+0

上面的例子是關閉App – SRY

+0

調試你的應用程序,並檢查數據是否發送到Web或否則可能是Web服務中的問題 –

0

如果你真的檢查了logcat,你會發現你已經達到了你想達到的目標。

您嘗試過: 發送HTTP POST請求進行登錄。 你有什麼反應: HTML頁面。

您的web應用程序不會爲特定的HTTP POST請求返回true/false。而您正在檢查true

解決方案:
1.解析HTML頁面,以確定成功登錄(不推薦)
2.重新設計你的web應用程序返回的XML/JSON響應爲好。您可以同時擁有網頁和可以從任何其他應用使用的API。

你應該照顧的其他事情:
1.切勿在UI線程上進行網絡調用。可能最終變得ANR
2.永遠不要留下沒有標籤的日誌語句。標籤使調試更容易。當你編寫大量代碼並處理大量複雜問題時,你會明白這一點。

希望這會有所幫助。