Android版的URLConnection并只在wifi的随着3G工作,而不是 [英] Android URLConnection does only work in wifi, not with 3g
问题描述
我写它使用一个URLConnection的得到一个.html文件的应用程序。
一切都通过WiFi工作正常。但在3G上的文件没有正确返回。
当我尝试通过浏览器访问该网站,它工作正常。
任何人有一个建议?
更新:
这里是我的code:
URL downloadUrl;
URLConnection的downloadConnection;
InputStream的InputStream的;
字节[] inputBytes;
的String []输出;
私人无效downloadSource(字符串金银丝)
{ 尝试
{
downloadUrl =新的URL(金银丝); downloadConnection = downloadUrl.openConnection();
downloadConnection.setConnectTimeout(10000);
downloadConnection.setReadTimeout(10000);
的InputStream = downloadConnection.getInputStream();
ByteArrayOutputStream结果=新ByteArrayOutputStream(); inputBytes =新的字节[10000]
INT I;
INT的i1 = 0;
而((ⅰ= inputStream.read(inputBytes))大于0)
{
result.write(inputBytes,0,I);
result.flush();
I1 + =我;
}
result.flush();
result.close();
输出= result.toString()分裂(\\ n); }
赶上(例外五)
{
e.printStackTrace();
}
}
也许这是迟到的回答一点点,但我有同样的问题,与WiFi下载的HTML有一个比随着3G下载不同的空间。
我解决它删除用户代理的连接:
的URLConnection康恩= url.openConnection();
conn.setRequestProperty(用户代理,);
我希望它可以帮助别人!
I have written an app which uses an URLConnection to get a .html file. Everything works fine over wifi. But over 3g the file is not returned correctly. When i try to access the website via the browser it works fine. Anyone has a suggestion?
Update: Here is my code:
URL downloadUrl;
URLConnection downloadConnection;
InputStream inputStream;
byte[] inputBytes;
String[] output;
private void downloadSource(String pUrl)
{
try
{
downloadUrl = new URL(pUrl);
downloadConnection = downloadUrl.openConnection();
downloadConnection.setConnectTimeout(10000);
downloadConnection.setReadTimeout(10000);
inputStream = downloadConnection.getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
inputBytes = new byte[10000];
int i;
int i1 = 0;
while ((i = inputStream.read(inputBytes)) > 0)
{
result.write(inputBytes, 0, i);
result.flush();
i1 += i;
}
result.flush();
result.close();
output = result.toString().split("\n");
}
catch (Exception e)
{
e.printStackTrace();
}
}
Maybe it's a little bit late for answer but I had the same problem, with Wifi the html downloaded had different spaces than the one downloaded with 3G.
I solved it deleting the User-Agent in the connection:
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","");
I hope it helps someone!
这篇关于Android版的URLConnection并只在wifi的随着3G工作,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!