CustomHttpClient.executeHttpPost给出ConnectTimeoutException

时间:2012-02-10 21:00:04

标签: php android timeout

请求我的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),但我现在还从我的代码中添加了一些代码段。

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();
    }
?>

1 个答案:

答案 0 :(得分:0)

  1. 通过从计算机上的常规浏览器访问,确保您指向的URL实际可用。
  2. 如果1)成功,请确保您可以从模拟器中的浏览器访问它。如果没有,请禁用计算机,然后仅使用无线功能重试。