我目前正在使用Android SDK和PHP服务器端脚本为我的网站开发应用程序。
我正在尝试通过username
和password
请求发送HttpClient
和HttpPost
等POST数据并获取结果(echo $result;
)。不幸的是,HttpResponse
无法正常运行。发出请求,结果由我的PHP脚本发回,但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";
}
?>
还有我的Java login()
方法:
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开始的,所以我不是最好的!
答案 0 :(得分:0)
您是否看到Toasts
?如果没有,那么我认为你可能有错误的context
这一行:
Toast.makeText(this, "Bad username/password", Toast.LENGTH_LONG);
你遗失了.show()
。
如果这是在视图中,则使用getContext()
或getBaseContext()
代替this
。
我建议您使用Log.d()
来记录响应,并检查它是否符合预期。