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();
}
走到
bReader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
就会报空指针错误。为什么呢 ?
----------------
<uses-sdk android:minSdkVersion="14"/> 改为 14以下版本。。。就OK。。。
分享到:
相关推荐
这里以HttpURLConnection为例: ```java URL url = new URL("http://example.com/image.jpg"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); ...
在Android开发中,由于安全性和用户体验的考虑,自Android 4.0(API级别14)开始,所有的网络操作必须在非主线程中执行,否则会抛出`NetworkOnMainThreadException`异常。这个规则被称为“应用后台执行”或“网络...
首先,我们要明白为什么在Android 4.0以上版本需要在网络操作中使用分线程。这是由于Android的主线程,也就是UI线程,不能长时间执行耗时操作,包括网络请求。如果在主线程中进行网络通信,可能会导致应用程序无响应...
本项目名为“Android中获取网页中的指定内容源码”,其核心功能就是使用JSOUP来解析网页并提取所需信息。 首先,我们需要了解JSOUP的基本概念。JSOUP提供了一个非常方便的API,使得开发者可以以类似于DOM、CSS以及...
- 通过`HttpURLConnection`的`getOutputStream()`发送请求参数,通过`getInputStream()`读取响应数据。 2. **示例代码**: ```java URL url = new URL("http://172.16.230.50/YourWebService.asmx/YourMethod");...
在Android 4.0(API Level 14)后,可以使用`executeOnExecutor()`方法选择线程池执行,提高并发性能。 6. **替代方案**: 随着Android版本的更新,开发者有更多的选择来替代AsyncTask,如IntentService、RxJava、...
首先,由于Android 4.0(API Level 14)之后的版本中,系统不允许在主线程中执行网络请求,因为这可能导致应用界面卡顿,影响用户体验。为了解决这个问题,我们需要在单独的线程中进行网络操作。在示例中,创建了一...
Android 开发使用 URLConnection 进行网络编程详解 Android 开发使用 URLConnection 进行网络编程是 Android 开发中的一种常见的网络编程方式。URLConnection 是 Java 中的一个类,用于表示应用程序和 URL 之间的...
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); ``` 对于GET请求,直接调用`connect()`方法即可发送请求并获取响应: ```java connection.connect(); `...