【发布时间】:2012-02-10 21:00:03
【问题描述】:
我在请求 login.php 时遇到了一些超时问题
02-10 20:23:26.361: V/(1084): org.apache.http.conn.ConnectTimeoutException: 连接到 /10.0.2.2:8082 超时
- 我和这个非常相似 (http://stackoverflow.com/questions/8435535/android-and-php-login-authentication) 但另外我现在从我的代码中添加了一些 sn-ps。
LoginActivity.java
response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/login/login.php",postParameters);
在 CustomHttpClient.java 中,我在最后执行后得到超时。
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
由于我使用了 5 秒后超时
public static final int HTTP_TIMEOUT = 5 * 1000;
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
在executeHttpPost方法中
我的 login.php 看起来像这样。
<?php
$hostname = 'localhost';
$username = 'root';
$pws = 'test';
try {
$name = $_POST['name'];
$pw = $_POST['pw'];
$dbh = new PDO("mysql:host=$hostname;dbname=toDoList", $username, $pws);
$stmt = $dbh->prepare("SELECT * FROM account WHERE name = '$name'");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row)
{
echo ($pw == $row['password'] ? '1' : '0');
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
【问题讨论】:
-
可以从设备的浏览器访问此页面吗?
-
我该如何测试这个? XAMPP 服务器在我的本地主机上运行,但如果它也可以从模拟器中获得,我真的不知道。一些解决方法来测试它?
-
顺便说一句,我自己修好了,只要让端口 8082 离开就可以了