HttpURLConnection 无法在仅 3g 的 wifi 上工作

发布于 2024-11-26 09:22:29 字数 1080 浏览 1 评论 0原文

我创建了一个通过互联网读取 json 文件的应用程序。 我的应用程序似乎可以通过 wifi 正常工作,但当我使用 3g 时,它在橙色网络上不起作用。我有一个朋友在不同的 3g 网络上尝试过,结果确实有效。我可以在 3g 上浏览互联网,所以我知道我有一个连接

,我添加了一些调试代码,发现它是一个 IOException 文件未找到(如果有帮助的话),但为什么它可以在 wifi 上找到它?

我的代码如下:

        String addr = "http://policeapi2.rkh.co.uk/api/crimes-street/all-crime?lat=" + strLat + "&lng=" + strLng;
        URL url = new URL(addr);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        String data = "user:password";
        String encoding = Base64.encodeToString(data.getBytes("US-ASCII"), Base64.DEFAULT);

        conn.setRequestProperty ("Authorization", "Basic " + encoding);

        conn.setRequestMethod("GET");

        conn.connect();

        InputStream in = conn.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = null;
        StringBuilder stringBuffer = new StringBuilder();

        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        } 

I have created a app that reads a json file across the internet.
My app seems to work fine over wifi but when i use 3g it does not work for me on the orange network. I have a friend who tried it on a different network over 3g and it did work. I can browse the internet on 3g so i know i have a connection

I have added some debug code and found that its a IOException file not found if that helps, but why can it find it on wifi?

My code is as follows:

        String addr = "http://policeapi2.rkh.co.uk/api/crimes-street/all-crime?lat=" + strLat + "&lng=" + strLng;
        URL url = new URL(addr);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        String data = "user:password";
        String encoding = Base64.encodeToString(data.getBytes("US-ASCII"), Base64.DEFAULT);

        conn.setRequestProperty ("Authorization", "Basic " + encoding);

        conn.setRequestMethod("GET");

        conn.connect();

        InputStream in = conn.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String line = null;
        StringBuilder stringBuffer = new StringBuilder();

        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        } 

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文