【问题标题】:Android 2.2 (level 8) httpclient.execute consistent timeoutAndroid 2.2(8级)httpclient.execute一致超时
【发布时间】:2011-09-11 17:35:43
【问题描述】:

我正在尝试接收来自 RESTful 服务的响应,但收到了超时。我能够在我的模拟器上连接浏览器,因为我已经在模拟设备上配置了一个接入点以通过代理(在工作中)。网络好像没问题。我添加了:

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

到 AndroidManifest.xml 文件。

代码如下:

public String getInputStreamFromUrl(String url) {
    String content = null;
    InputStream stream = null;
    try {
        HttpGet httpGet = new HttpGet(url);
        HttpClient httpclient = new DefaultHttpClient();
        // Execute HTTP Get Request
        HttpResponse response = httpclient.execute(httpGet);
        stream = response.getEntity().getContent();
        BufferedReader rd = new BufferedReader(new InputStreamReader(stream), 4096);
        String line;
        StringBuilder sb =  new StringBuilder();
        while ((line = rd.readLine()) != null) {
                sb.append(line);
        }
        rd.close();
        content = sb.toString();
            } catch (Exception e) {
        content = e.getMessage();
    }
    return content;

我知道我应该返回一个流,但是为了在 TextView 小部件中显示一些字符串值就足够了,所以我只是使用字符串进行实验。无论传递什么 URL,它始终挂在 .execute 上。我也通过了有效的 IP,什么都没做。

提前感谢您的帮助。

【问题讨论】:

  • 作为对此的更新,我能够在代理之外连接此代码,即直接连接。我是否需要清单中列出的其他权限或某些特定指令才能使用代码中的代理?看起来肯定是代理把事情搞砸了。

标签: java android android-emulator timeout


【解决方案1】:

试试这个。把它放在班级的首位。

System.setProperty("http.proxyHost", <your proxy host name>);
System.setProperty("http.proxyPort", <your proxy port>);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多