【发布时间】:2015-09-15 13:58:41
【问题描述】:
我需要为用户登录认证制作 andoird 应用程序。
这是我的 php 代码:
<?php
$con = mysql_connect('ip','root','pass') or die('cannot connect to db');
mysql_select_db('master1') or die('cannot select db');
if(isset($_POST['email']) && isset($_POST['pass'])) {
$email = $_POST['email'];
$pass = $_POST['pass'];
$query = "SELECT * FROM Users WHERE user_email='$email' AND user_password='$pass'";
$res=mysql_query($query);
}
if(mysql_num_rows($res) > 0)
echo "You are successfully logged in";
else
echo "Incorrect email or password";
这是我的安卓代码:
@Override
protected String doInBackground(String... params) {
InputStream is1 = null;
for(String url1:params) {
try {
ArrayList<NameValuePair> pairs = new ArrayList<>();
pairs.add(new BasicNameValuePair("email", etEmail.getText().toString()));
pairs.add(new BasicNameValuePair("pass", etPass.getText().toString()));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url1);
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
is1 = response.getEntity().getContent();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is1, "UTF-8"), 8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
}
String line = null;
try {
while ((line=reader.readLine())!=null) {
text += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
}
try {
is1.close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
}
}
return text;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
if (s.contains("You are successfully logged in")){
Intent i = new Intent(LoginActivity.this,ConnectActivity.class);
i.putExtra("user_email",etEmail.getText().toString());
startActivity(i);
}
dialog.dismiss();
}
}
问题是我在我的应用程序中获得了整个 php 脚本作为返回结果,我只需要 php echo,它可以有两个可能的值,正如您在我的代码中看到的那样,具体取决于用户插入的值。
有谁知道我的代码可能是什么问题,是在 php 部分还是在 android 部分???
【问题讨论】:
-
你确定你有一个 web 服务器来提供 php 文件吗?听起来你只是在阅读 php 文件的内容。
-
听起来您的网络服务器没有尝试解析 PHP。看看this question对你有没有帮助。
-
您必须使用 json 编码而不是 echo,并在 android 中使用字符串生成器和流阅读器。
-
我认为问题出在网络服务器上,当我修复该应用程序正常工作时
标签: php android login android-asynctask