02-10 20:23:26.361:V /(1084):org.apache.http.conn.ConnectTimeoutException:连接到/10.0.2.2:8082超时
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方法
中<?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();
}
?>
答案 0 :(得分:0)