Android的HTTP POST登录 [英] Android http Post Login
问题描述
我想通过一个Android应用程序,使登录到我的网站。出于某种原因,总有什么不顺心。
I'm trying to make a login to my website via an Android app. For some reason something always goes wrong.
这是我的网站登录结构:
的login.php - 登录表单(用户名和放大器;密码)
Login.php - the login form (username & password)
Auth.php - 登录验证页面(获取用户名和放大器;密码的页面 - login.php中(POST方法))
我已经有用户名和放大器接口;密码(的EditText
两者)
Auth.php - login authentication page (gets the username & password from page - login.php (POST method))
I already have to interface with Username & Password (EditText
both)
什么是通过Android的应用程序,使一个登录的方法?
What is the method to make a login via android app?
这是我当前的登录方法:
private void login() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://url/auth.php");
JSONObject jsonObject = new JSONObject();
try {
List nameValuePairs = new ArrayList(2);
jsonObject.put("action", "login");
jsonObject.put("username", "user1");
jsonObject.put("password", "12345");
nameValuePairs.add(new BasicNameValuePair("jsonString", jsonObject.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
String responseStr = org.apache.http.util.EntityUtils.toString(response.getEntity());
statusText.setText(responseStr);
} catch (ClientProtocolException e) {
statusText.setText(e.getLocalizedMessage());
} catch (IOException e) {
statusText.setText(e.getLocalizedMessage());
} catch (JSONException e) {
}
}
编辑:
我合并两页(登录&安培; AUTH)到一个页面上,当我调用该方法,responseStr得到整个页面的html code
I merged both pages (login & auth) to one page, when I call the method, responseStr get the entire page html code.
感谢
推荐答案
您不能toString方法登录响应主体。
You can't log the response body with the toString method.
可以在responseEntity返回的对象理解为一个InputStream或者更简单地说,转换为字符串与EntityUtils类。
The object returned in the responseEntity can be read as an InputStream or more simply, converted to a string with the EntityUtils class.
String responseStr = org.apache.http.util.EntityUtils.toString(response.getEntity());
尝试登录的responseStr。
Try logging the responseStr.
这篇关于Android的HTTP POST登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!