Android 4.0 HttpUrlConnection的getInputStream()方法总是返回空的问题
2013-04-26 22:14
519 查看
Java代码
public String downTxt(String urlStr) {
StringBuffer sBuffer = new StringBuffer();
String line = null;
BufferedReader bReader = null;
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
bReader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = bReader.readLine()) != null) {
sBuffer.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sBuffer.toString();
}
走到
Java代码
bReader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
就会报空指针错误。为什么呢 ?
----------------
<uses-sdk android:minSdkVersion="14"/> 改为 14以下版本。。。就OK。。。不行的话呢 再试试其他的sdk版本,总之就是这个地方的问题
---------------------------------------------------------------------------------
经测试,7,8,9三个版本是可以的,同时,去掉android:targetSdkVersion
public String downTxt(String urlStr) {
StringBuffer sBuffer = new StringBuffer();
String line = null;
BufferedReader bReader = null;
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
bReader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = bReader.readLine()) != null) {
sBuffer.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sBuffer.toString();
}
走到
Java代码
bReader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
就会报空指针错误。为什么呢 ?
----------------
<uses-sdk android:minSdkVersion="14"/> 改为 14以下版本。。。就OK。。。不行的话呢 再试试其他的sdk版本,总之就是这个地方的问题
---------------------------------------------------------------------------------
经测试,7,8,9三个版本是可以的,同时,去掉android:targetSdkVersion
相关文章推荐
- android 4.0 HttpURLConnection. getInputStream() 为空的问题
- android HttpURLConnection.getInputStream()问题
- HttpURLConnection 因Android版本不同而导致的Bad Request问题,在2.0+不行,4.0+可以
- Android 6.0 使用HttpURLConnection 使用Get提交遇到405等问题。
- Android HttpURLConnection.getResponseCode()错误解决方法
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- 工作学习 问题总结 java httpurlconnection.getinputstream
- 使用HttpURLConnection的getContentLength()方法返回-1??
- Android使用HttpUrlConnection get请求带中文参数的编码问题
- 关于inputStream.available()与HttpURLConnection.getContentLength()方法获取下载文件的大小
- 关于inputStream.available()与HttpURLConnection.getContentLength()方法获取下载文件的大小
- Android网络通信技术之基于HttpURLConnection的POST/GET传值问题
- android中HttpURLConnection调用getResponseCode()时崩溃 解决方法
- httpurlconnection post 和GET 分别传递2个参数给后台,返回JSON 解决乱码问题
- Android HttpUrlConnection 的“POST”和"GET"方法学习心得
- HttpURLConnection getContentLength();返回时-1或者是0
- HttpUrlConnection和HttpClient和android-async-http框架的GET和POST请求
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android网络连接的两种方法:apache client和httpurlconnection的比较