java.net.SocketException权限被拒绝

时间:2013-01-09 14:35:54

标签: android sockets

大家好我想尝试使用以下代码阅读网站内容

public class Add extends MenuActivity {

    Button submit;
    EditText editText;
    private WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        String contents = urlToString("http://www.vogella.com");

        submit = (Button) findViewById(R.id.button1);
        editText = (EditText) findViewById(R.id.editText1);
//        String addUrl = editText.getText().toString();
        submit.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                Log.d("Submit"," Pressed");
                Log.d("EditText",editText.getText().toString());
            }

        });

    }


    private String urlToString(String address) {
        HttpURLConnection con = null;
        URL url;
        InputStream is = null;
        try {
            url = new URL(address);
            con = (HttpURLConnection) url.openConnection();
            con.setReadTimeout(10000);
            con.setConnectTimeout(15000);
            con.setRequestMethod("GET");
            con.connect();
            is = con.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(is));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                Log.d("HTTP", line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }


}

但是我收到以下错误,而且我不知道如何解决它。

01-09 14:27:54.405: W/System.err(637): java.net.SocketException: Permission denied
01-09 14:27:54.416: W/System.err(637):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
01-09 14:27:54.416: W/System.err(637):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
01-09 14:27:54.416: W/System.err(637):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
01-09 14:27:54.416: W/System.err(637):  at java.net.Socket.checkClosedAndCreate(Socket.java:873)
01-09 14:27:54.461: W/System.err(637):  at java.net.Socket.connect(Socket.java:1020)
01-09 14:27:54.461: W/System.err(637):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
01-09 14:27:54.461: W/System.err(637):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
01-09 14:27:54.461: W/System.err(637):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
01-09 14:27:54.461: W/System.err(637):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
01-09 14:27:54.466: W/System.err(637):  at uk.ac.tees.L1087591.Add.urlToString(Add.java:74)
01-09 14:27:54.466: W/System.err(637):  at uk.ac.tees.L1087591.Add.onCreate(Add.java:33)
01-09 14:27:54.466: W/System.err(637):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-09 14:27:54.466: W/System.err(637):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-09 14:27:54.466: W/System.err(637):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-09 14:27:54.466: W/System.err(637):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-09 14:27:54.466: W/System.err(637):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-09 14:27:54.466: W/System.err(637):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 14:27:54.466: W/System.err(637):  at android.os.Looper.loop(Looper.java:123)
01-09 14:27:54.466: W/System.err(637):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-09 14:27:54.466: W/System.err(637):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 14:27:54.466: W/System.err(637):  at java.lang.reflect.Method.invoke(Method.java:521)
01-09 14:27:54.476: W/System.err(637):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-09 14:27:54.476: W/System.err(637):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-09 14:27:54.476: W/System.err(637):  at dalvik.system.NativeStart.main(Native Method)

如果您有任何想法,请不要害羞。

3 个答案:

答案 0 :(得分:3)

我认为你必须使用

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

答案 1 :(得分:2)

我猜你错过了清单文件中的这个权限

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

答案 2 :(得分:1)

我在AndroidManifest.xml文件中也有这一行

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