Android httpGet 上的 URL 重写输入流读取器

发布于 2024-11-16 17:47:33 字数 1497 浏览 3 评论 0原文

如果我有这段代码并且 url 是 "http://www.example.com/someFile.txt" 一切正常。但是如果我输入 "http://www.example.com/export/something" (在服务器上的 URL rewrite/.htaccess 的帮助下),桌面浏览器中再次在 Android 中输出相同的结果我收到错误 Error w/file: End of input at character 0 of

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();

    String result = "";
    HttpGet httpGet = new HttpGet("http://www.example.com/export/notWorking");
    HttpResponse response = null;
    try {
        response = httpClient.execute(httpGet, localContext);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = null;
        while ((line = reader.readLine()) != null){
            result += line;
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

急需解决此问题吗?

编辑:使用用户代理提供帮助

    HttpParams params = new BasicHttpParams();  
    params.setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71");

没有帮助。

If I have this piece of code and url is "http://www.example.com/someFile.txt" everything works just fine. But if I put "http://www.example.com/export/something" (with help of URL rewrite/.htaccess on the server) which in desktop browser again outputs the same result in Android I get error Error w/file: End of input at character 0 of

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();

    String result = "";
    HttpGet httpGet = new HttpGet("http://www.example.com/export/notWorking");
    HttpResponse response = null;
    try {
        response = httpClient.execute(httpGet, localContext);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = null;
        while ((line = reader.readLine()) != null){
            result += line;
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Hot to solve this problem?

EDIT: Help with user agent

    HttpParams params = new BasicHttpParams();  
    params.setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71");

doesn't help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-11-23 17:47:33

解决方案 - 非常琐碎我必须说 - 我删除了模拟器,创建了一个新模拟器,然后一切都工作正常......所以将来 - 仅在设备上进行测试本身:)

Solution - pretty trivial I must say - I deleted the emulator, created a new one and then everything is working just fine...so in future - test only on the device itself :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文