Android的HTTP登录后 [英] Android Http Login Post
问题描述
我想通过anAndroid应用,使登录到我的网站。
出于某种原因,它总是无法登录。
I'm trying to make a login to my website via anAndroid app. For some reason it always fail to login.
下面是我对应用程序登录方法:
Here's my login method for the app:
private void login() {
//this method returns false
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite.com/mobileauth.php");
JSONObject jsonObject = new JSONObject();
try {
List nameValuePairs = new ArrayList(2);
jsonObject.put("username", "user1");
jsonObject.put("password", "12345");
nameValuePairs.add(new BasicNameValuePair("jsonString", jsonObject.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String responseStr = org.apache.http.util.EntityUtils.toString(response.getEntity());
statusText.setText(responseStr);
} catch (ClientProtocolException e) {
statusText.setText(e.toString());
} catch (IOException e) {
statusText.setText(e.toString());
} catch (JSONException e) {
statusText.setText(e.toString());
}
}
我创建了移动设备登录特定的文件如Android。
I created a specific file for mobile device login such as Android.
下面是在服务器上我的认证文件:
Here's my authentication file on the server:
$host="*"; // Host name
$username="*"; // Mysql username
$password="*"; // Mysql password
$tbl_name="users"; // Table name
$db_name="database";
$link = mysql_connect($host, $username, $password) or die("Connection Error");
$db = mysql_select_db("$db_name")or die("Connection Error");
$myusername = $_REQUEST['username'];
$mypassword = $_REQUEST['password'];
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' AND paswd='$mypassword'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count == 1) {
$timeUpdateQuesry="UPDATE users SET lastLogin=Now() WHERE username='$myusername' AND paswd='$mypassword'";
mysql_query($timeUpdateQuesry);
echo("true");
} else {
//happend without leaving page
echo ("false");
}
由于某种原因,我总是假
我的状态TextView中。
For some reason I always get false
in my status TextView.
我将AP preciate任何帮助
I'll appreciate any help
谢谢,
推荐答案
从Android code取出所有的JSON。你不接受它作为JSON在PHP端,所以这可能是一个根本原因。该namevaluepairs中应该工作正常。
Remove all the JSON from your android code. You're not receiving it as JSON on the PHP side, so this is probably a root cause. The NameValuePairs should work fine.
这篇关于Android的HTTP登录后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!