当前位置: 代码迷 >> Android >> 关于anroid中从网上下载文件的有关问题!
  详细解决方案

关于anroid中从网上下载文件的有关问题!

热度:52   发布时间:2016-04-28 05:22:59.0
关于anroid中从网上下载文件的问题!!!
麻烦大家帮忙看一下这个代码,地址是绝对没有问题的,Manifast.xml文件中我也增加了可访问INTERNET的代码,报错是在buffer.close()的空指针错误,但是我调试的时候每次都是在urlcon.getInputStream()那里就被catch住了。

MainActivity中的click事件

class downtext implements OnClickListener {
@Override
public void onClick(View v) {

HttpDownloader hdl = new HttpDownloader();
String s = hdl
.download("http://172.31.23.93:8080/web01/abc.txt");
System.out.println(s);
}
}


HttpDownloader中的download

public String download(String txstring) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
url = new URL(txstring);
HttpURLConnection urlCon = (HttpURLConnection) url
.openConnection();
buffer = new BufferedReader(new InputStreamReader(
urlCon.getInputStream()));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

urlCon.connect()
如果是这里的话那就是这个被catch住了

这里catch报null错误?
不是 ,我换成每一个都try catch后UrlCon.connect()这里的堆栈信息没有打印出来,倒是进入while循环的时候报的null错误


你好好检查一下那个url吧,你这根本不是Null的错误,而是超时。。。。  buffer根本就没创建出来,finally代码块里去close肯定是null啊