我有一個問題,我想通過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"/>
它只是返回登錄頁面。它不傳遞參數登錄。有什麼建議麼 ?
是的,但它不適合我。我嘗試了很多次。但它只是返回登錄頁面。我想在登錄後。它重定向到主頁。但它總是返回login.php: - ? –
如果你得到正確的結果,那麼你需要移動到下一頁使用[意向](http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/) –
不,我的意思是。它只是返回登錄頁面。它不傳遞數據登錄。我不知道爲什麼會發生什麼? –