程序是要从远程服务器上下载xml并解析,用wifi接入就没问题,一换成gprs(cmnet)就会连接timeout(cmnet绝对可以上网,其他程序也没问题的),logcat显示其中一个system err为java.net.SocketTimeoutException:Connection timed out,下载不到xml文件,这是怎么回事? 我连接服务器就是用的HttpURLConnection接口而已啊,莫名奇妙了,抓狂了,求好心人指点下面是download代码:
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
// 创建一个URL对象
url = new URL(urlStr);
// 创建一个Http连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 使用IO流读取数据
buffer = new BufferedReader(new InputStreamReader(urlConn
.getInputStream()));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
// 创建一个URL对象
url = new URL(urlStr);
// 创建一个Http连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 使用IO流读取数据
buffer = new BufferedReader(new InputStreamReader(urlConn
.getInputStream()));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
就是这句过不去,加入urlConn判断,也不是空,,,他nnd,为毛wifi可以,cmnet就不可以
public String download(String urlStr) {
System.out.println(urlStr);
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try{
// 创建一个URL对象
url = new URL(urlStr);
}catch(MalformedURLException e){
Log.e(TAG,"MalformedURLException");
}
if(url!=null){
try {
// 创建一个Http连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// urlConn.connect();
urlConn.setConnectTimeout(60000);
// 使用IO流读取数据
Log.d(TAG, "正在尝试下载");
buffer = new BufferedReader(new InputStreamReader(urlConn
.getInputStream()));
while ((line = buffer.readLine()) != null) {
Log.d(TAG, "下载成功");
sb.append(line);
}
} catch (IOException e) {
Log.e(TAG, "IOException");
} finally {
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}else
{Log.e(TAG,"Url NULL");
return null;
}
}
恩,只能这样,我试试吧java标准接口还真是不好用,谢谢
哎,彻底无语,换成HttpClient接口,还是wifi下ok, cmnet报IOException,我想放弃了。
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
HttpGet httpRequest = new HttpGet(urlStr);
try {
// 取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpresponse = httpclient.execute(httpRequest);
if (httpresponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
//取得返回的字符串
strResult = EntityUtils.toString(httpresponse.getEntity());
//解决HttpClient接收XML内含中文乱码问题
strResult = new String(strResult.getBytes("ISO-8859-1"), "UTF-8");
}
}catch (ClientProtocolException e)
{
Log.e(TAG,"ClientProtocolException");
}catch (IOException e){
Log.e(TAG,"IOException");
}
return strResult;
}
<uses-permission android:name="android.permission.INTERNET" />
介个没有问题吧。。
奇怪的是,如果我用湖南的卡UC浏览器却都能访问这个页面,而湖南联通HttpURLConnection却无法连接。
以此希望对后来人有帮助!