1

Hi everyone I am trying to read the content of a website with the following code

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();
    }


}

But I get the following error and I don't know how to solve it.

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)

If you have any ideas don't be shy.

1

3 Answers 3

4

I think you must use

    <uses-permission android:name="android.permission.INTERNET"/>
Sign up to request clarification or add additional context in comments.

1 Comment

In case anyone still having this problem even after adding this permission on the android manifest file. Then probably you should clean and rebuild the project.
3

i guess you missed this permission into manifest file

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

Comments

1

I also have this line in the AndroidManifest.xml file

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

1 Comment

this doesn't needed for this problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.