2013-01-23 75 views
0

我有一個問題,我想通過Android設備登錄一個網站。我已經看過很多這方面的樣本,但它不適合我。這是我的代碼:http post數據登錄不起作用?

DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://www.example.com/login.php"); 
    httppost.setHeader("Content-Type","text/html"); 
    ArrayList<BasicNameValuePair> pairs = new ArrayList(); 
    pairs.add(new BasicNameValuePair("username", "test1")); 
    pairs.add(new BasicNameValuePair("password", "pass1")); 
    try { 
     UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, HTTP.UTF_8); 

     /** Assign the POST data to the entity */ 
     httppost.setEntity(p_entity); 

     /** Perform the actual HTTP POST */ 
     HttpResponse response = httpclient.execute(httppost); 

     TextView txtView = (TextView)findViewById(R.id.txtMessage); 
     txtView.setText(EntityUtils.toString(response.getEntity())); 

    } catch (UnsupportedEncodingException uee) { 
     uee.printStackTrace(); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 

我不明白新的BasicNameValuePair(「username」,「test1」)。什麼是attibute這裏?

<input type="text" name="username" size="25" maxlength="40" value="" class="post" id="autofocus" tabindex="1"/> 

<input type="password" name="password" size="25" maxlength="32" class="post" tabindex="2"/> 

它只是返回登錄頁面。它不傳遞參數登錄。有什麼建議麼 ?

回答

0

test1是您的用戶名,pass1是您的密碼。

如果你正在從edittexts的用戶名和密碼,然後使用此代碼

pairs.add(new BasicNameValuePair("username", ed_user.getText().toString())); 
pairs.add(new BasicNameValuePair("password", ed_pass.getText().toString())); 

其中ed_usered_pass是你edittexts

+0

是的,但它不適合我。我嘗試了很多次。但它只是返回登錄頁面。我想在登錄後。它重定向到主頁。但它總是返回login.php: - ? –

+0

如果你得到正確的結果,那麼你需要移動到下一頁使用[意向](http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/) –

+0

不,我的意思是。它只是返回登錄頁面。它不傳遞數據登錄。我不知道爲什麼會發生什麼? –

0

檢查HTML頁面的源代碼,並查找以下部分(例如):

<form name="frm" method="post" action="/cgi-bin/websms/index.pl" onSubmit="return validate()"> 

現在根據表單的action =「...」部分更新您的URL。