简体   繁体   中英

Why am I getting a “Permission Denied” error?

I'm making an Android application that has to read some values from a JSON object. So to get the JSON object and read it, I have used the following method:

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) {
    }

}

And the jRatesURL String is this:

private static final String jRatesURL = "http://openexchangerates.org/latest.json";

But when executing this method, I get a "Permission Denied" error:

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)

So what is the cause of this error? Am I doing something wrong when connecting to the JSON link?

Any help greatly appreciated. Thanks!

from here

String INTERNET Allows applications to open network sockets.

Use that permission in your manifest

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

Have you given the permission

<uses-permission android:name="android.permission.INTERNET" /> 

in the manifest?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM