我正在制作一个必须从JSON对象中读取一些值的Android应用程序。因此,为了获取JSON对象并读取它,我使用了以下方法:
private void _init() {
_rates = new HashMap<String, String>();
URLConnection jsonURL = null;
InputStream in = null;
String jRatesStream = null;
try {
jsonURL = new URL(jRatesURL).openConnection();
jsonURL.connect(); // ERROR OCCURING HERE
in = jsonURL.getInputStream();
jRatesStream = _convertStreamToString(in);
} catch(Exception e) {
Log.v("ERROR", "ERROR WHILE READING URL", e);
} finally {
try {
in.close();
} catch (Exception e) {
}
}
try {
JSONObject jObject = new JSONObject(jRatesStream);
JSONObject jRates = jObject.getJSONObject("rates");
Iterator<String> iterator = jRates.keys();
while(iterator.hasNext()) {
String key = iterator.next();
_rates.put(key, String.valueOf(jRates.getDouble(key)));
}
} catch (Exception e) {
}
}
jRatesURL
字符串是这样的:
private static final String jRatesURL = "http://openexchangerates.org/latest.json";
但是在执行此方法时,我收到“Permission Denied”错误:
ERROR WHILE READING URL
java.net.SocketException: Permission denied
at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
at java.net.Socket.checkClosedAndCreate(Socket.java:873)
at java.net.Socket.connect(Socket.java:1020)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
at com.testing.exchangerates.Rates._init(Rates.java:45)
at com.testing.exchangerates.Rates.<init>(Rates.java:27)
at com.testing.exchangerates.HomeActivity.onCreate(HomeActivity.java:27)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
那么这个错误的原因是什么?连接到JSON链接时我做错了吗?
任何帮助非常感谢。谢谢!
答案 0 :(得分:2)
来自here
字符串INTERNET允许应用程序打开网络套接字。
在清单中使用该权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
答案 1 :(得分:1)
您是否已获得许可
<uses-permission android:name="android.permission.INTERNET" />
清单中的?