我目前正在使用 Android SDK 和 PHP 服务器端脚本为我的网站开发应用程序。
我正在尝试通过 a和请求发送 POST 数据username
并获取结果()。不幸的是,它无法正常工作。请求已发送,结果由我的 PHP 脚本发回,但尚未工作。password
HttpClient
HttpPost
echo $result;
HttpResponse
Intent
这是我的 PHP 代码(我隐藏了我的数据库信息,你会明白为什么!):
<?php
$con = mysql_connect('localhost','','');
mysql_select_db('', $con);
$username = $_POST['username'];
$password = $_POST['password'];
$users = mysql_query("SELECT * FROM `` WHERE `username`='".$username."' AND `password`='".$password."'");
if (mysql_num_rows($users) == 1)
{
$usr = mysql_fetch_array($users);
$usr['username'];
}
else
{
echo "error";
}
?>
还有我的 Javalogin()
方法:
public void login()
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.creationsmicroweb.com/app/login.php");
String username = (String) findViewById(R.id.usr1).toString();
String password = (String) findViewById(R.id.pwd1).toString();
Button login = (Button) findViewById(R.id.button1);
try
{
// Disabling other request
login.setEnabled(false);
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("http", "Executing HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String status = inputStreamToString(response.getEntity().getContent()).toString();
Integer code = response.getStatusLine().getStatusCode();
if (!status.toString().equalsIgnoreCase("error"))
{
Intent myIntent = new Intent(this.getBaseContext(), HomeActivity.class);
myIntent.putExtra("username", status);
startActivityForResult(myIntent, 0);
Toast.makeText(this, "You've been connected!", Toast.LENGTH_LONG).show();
Log.i("login", "Logged in // " + response);
}
else
{
Toast.makeText(this, "Bad username/password", Toast.LENGTH_LONG);
Log.i("login", "Bad username/password // " + response);
login.setEnabled(true);
}
}
catch (ClientProtocolException e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("http", e.getMessage());
login.setEnabled(true);
}
catch (IOException e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("http", e.getMessage());
login.setEnabled(true);
}
}
private Object inputStreamToString(InputStream is)
{
// TODO Auto-generated method stub
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try
{
while ((line = rd.readLine()) != null)
{
total.append(line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
// Return full string
return total;
}
PHP 返回的响应无法与我的 Java 代码一起正常工作。很明显,当用户按下“登录”时,凭据被发送到 PHP,然后 PHP 返回一个答案(用户名或错误)。之后,Java 代码检查答案是否等于“错误”,但这不起作用。登录成功(我记录了所有内容),但 Intent 方法没有执行,并且没有显示 Toast 消息。
很明显,我想知道为什么请求不能正常工作。我从 Java 开始,所以我不是最好的!