android模拟器没有连接到开发REST Web服务ETIMEDOUT

时间:2014-03-15 22:46:44

标签: android rest web service emulation

我的清单文件中有以下权限

<uses-permission android:name="android.permission.INTERNET"/>
<user-permission android:name="android.permission.ACCESS_NETWORK_STATE"></user-permission>

我尝试过localhost,当然没有工作,但后来我尝试了10.0.2.2但仍然无法正常工作。我收到了ETIMEDOUT错误。

 StringBuilder stringBuilder = new StringBuilder();
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    try
    {
        HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200)
        {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null)
            {
                stringBuilder.append(line);
            }
            inputStream.close();
        }
        else
        {
            Log.d("JSON", "Failed to download file");
        }
    }
    catch (Exception e)
    {
        Log.d("readJSONFeed", e.getLocalizedMessage());
    }
    return stringBuilder.toString();

httpClient.execute部分失败

2 个答案:

答案 0 :(得分:0)

您将需要添加更多代码,以便我可以看到究竟是什么失败但通常我认为您需要添加 -

httpGet .setEntity("Some entity");

“某个实体” - 应该是您要发送的参数,如果您没有,则为null。

答案 1 :(得分:0)

我无法让它在IIS Express中运行,但随后将其连接到IIS Local并且它可以正常工作,如图所示。