HttpURLConnection 无法在仅 3g 的 wifi 上工作
我创建了一个通过互联网读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论