con.getInputStream()在Android项目抛出异常,如何解决? [英] con.getInputStream() throw exception in Android project, how to fix?
本文介绍了con.getInputStream()在Android项目抛出异常,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
结果
con.getInputStream()抛出异常,当运行在Android上的code,
但复制code到常规的Java项目是工作的时候,结果
抛出的对象E含有detailMessage =权限被拒绝。
我的code:
con.getInputStream() throw exception, when the code running on android,
but when copy the code to regular java project it is working,
The thrown object e contain "detailMessage = Permission denied".
my code::
public static HttpData get(String sUrl) {
HttpData ret = new HttpData();
String str;
StringBuffer buff = new StringBuffer();
try {
URL url = new URL(sUrl);
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((str = in.readLine()) != null) {
buff.append(str);
}
ret.content = buff.toString();
//get headers
Map<String, List<String>> headers = con.getHeaderFields();
Set<Entry<String, List<String>>> hKeys = headers.entrySet();
for (Iterator<Entry<String, List<String>>> i = hKeys.iterator(); i.hasNext();) {
Entry<String, List<String>> m = i.next();
Log.w("HEADER_KEY", m.getKey() + "");
ret.headers.put(m.getKey(), m.getValue().toString());
if (m.getKey().equals("set-cookie"))
ret.cookies.put(m.getKey(), m.getValue().toString());
}
} catch (Exception e) {
Log.e("HttpRequest", e.toString());
}
return ret;
}
推荐答案
添加&LT;使用许可权的android:NAME =android.permission.INTERNET对/&GT;
标记体现
将解决这个问题。
由于JEMS评论。
adding<uses-permission android:name="android.permission.INTERNET"/>
tag to manifest
will fix the problem.
Thanks to Jems comment.
这篇关于con.getInputStream()在Android项目抛出异常,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文