我是想在网络环境中下载读取一个文件,但不知道为什么报错,我具体是这样做的:
HttpDownloader httpDownloader = new HttpDownloader(); // 后面有贴出该类使用到的方法
String contents = httpDownloader.downloadBaseFile("http://192.168.1.104:8081/CFM_xfire/I_Remember.lrc");
System.out.println(contents);
其中这个是我的ip地址:192.168.1.104 ,我在tomcat中部署了CFM_xfire项目,然后项目下面有I_Remember.lrc文件,想通过程序把文件中的内容读取出来!
上面代码的 httpDownloader.downloadBaseFile方法如下:
public String downloadBaseFile(String urlStr){
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader reader = null;
try{
//创建一个url对象
url = new URL(urlStr);
//通過url对象,创建一个HttpURLConnection对象(连接)
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
//通过HttpURLConnection对象,得到InputStream
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
//使用io流读取文件
while ((line = reader.readLine()) != null){
sb.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if (reader != null){
reader.close();
}
} catch (Exception e){
e.printStackTrace();
}
}
return sb.toString();
}
但是提示是在 urlConnection.getInputStream()的时候报错,信息如下:
在本地我访问地址:http://192.168.1.104:8081/CFM_xfire/I_Remember.lrc 的时候是可以看到浏览器上显示信息的,大家看有没有遇到过这个问题!有的话还望赐教!
03-09 10:34:01.500: D/gralloc_goldfish(956): Emulator without GPU emulation detected.
03-09 10:35:53.625: W/System.err(956): android.os.NetworkOnMainThreadException
03-09 10:35:53.652: W/System.err(956): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
03-09 10:35:53.661: W/System.err(956): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:74)
03-09 10:35:53.670: W/System.err(956): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
03-09 10:35:53.670: W/System.err(956): at libcore.io.IoBridge.connect(IoBridge.java:112)
03-09 10:35:53.670: W/System.err(956): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-09 10:35:53.670: W/System.err(956): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
03-09 10:35:53.690: W/System.err(956): at java.net.Socket.connect(Socket.java:842)
03-09 10:35:53.690: W/System.err(956): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
03-09 10:35:53.710: W/System.err(956): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-09 10:35:53.810: W/System.err(956): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
03-09 10:35:53.820: W/System.err(956): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
03-09 10:35:53.820: W/System.err(956): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-09 10:35:53.820: W/System.err(956): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-09 10:35:53.820: W/System.err(956): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
03-09 10:35:53.845: W/System.err(956): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-09 10:35:53.852: W/System.err(956): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-09 10:35:53.852: W/System.err(956): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
03-09 10:35:53.860: W/System.err(956): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
03-09 10:35:53.870: W/System.err(956): at com.senly.utils.HttpDownloader.downloadBaseFile(HttpDownloader.java:37)
03-09 10:35:53.910: W/System.err(956): at com.senly.download.DownloadActivity$DownloadLRCListener.onClick(DownloadActivity.java:40)
03-09 10:35:53.910: W/System.err(956): at android.view.View.performClick(View.java:3480)
03-09 10:35:53.921: W/System.err(956): at android.view.View$PerformClick.run(View.java:13983)
03-09 10:35:53.921: W/System.err(956): at android.os.Handler.handleCallback(Handler.java:605)
03-09 10:35:53.930: W/System.err(956): at android.os.Handler.dispatchMessage(Handler.java:92)
03-09 10:35:53.930: W/System.err(956): at android.os.Looper.loop(Looper.java:137)
03-09 10:35:53.950: W/System.err(956): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-09 10:35:53.950: W/System.err(956): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 10:35:53.960: W/System.err(956): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 10:35:53.960: W/System.err(956): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-09 10:35:53.980: W/System.err(956): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-09 10:35:53.980: W/System.err(956): at dalvik.system.NativeStart.main(Native Method)
试下先调用outputstream回复<aclass='referer'target='_blank'>@Mr-Right:http://www.cnblogs.com/guodongli/archive/2011/04/05/2005930.htmloutputstream?我不需要写入什么外部存储或者其他的,用不到outputstream啊,你的意思是?
最后在51CTO技术论坛被版主帮忙解决了,不得不说那边神速啊!
要是同样遇到这个问题的朋友,可以去看看原因:
<arel="nofollow"><arel="nofollow">http://bbs.51cto.com/viewthread.php?tid=1020577&pid=5153749&page=1&extra<arel="nofollow">=
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。