15

I create my android application to link "www.google.com" . But I always get the Exception like that:

07-20 16:50:31.137: W/System.err(19007): java.net.SocketException: Permission denied
07-20 16:50:31.137: W/System.err(19007):    at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
07-20 16:50:31.137: W/System.err(19007):    at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
07-20 16:50:31.137: W/System.err(19007):    at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:219)
07-20 16:50:31.137: W/System.err(19007):    at java.net.Socket.checkOpenAndCreate(Socket.java:832)
07-20 16:50:31.137: W/System.err(19007):    at java.net.Socket.connect(Socket.java:978)
07-20 16:50:31.137: W/System.err(19007):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-20 16:50:31.137: W/System.err(19007):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
07-20 16:50:31.137: W/System.err(19007):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-20 16:50:31.147: W/System.err(19007):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-20 16:50:31.147: W/System.err(19007):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
07-20 16:50:31.147: W/System.err(19007):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-20 16:50:31.147: W/System.err(19007):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-20 16:50:31.147: W/System.err(19007):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-20 16:50:31.147: W/System.err(19007):    at com.xenos.httpclienttest.HttpClientTest$1.onClick(HttpClientTest.java:60)
07-20 16:50:31.147: W/System.err(19007):    at android.view.View.performClick(View.java:2532)
07-20 16:50:31.147: W/System.err(19007):    at android.view.View$PerformClick.run(View.java:9291)
07-20 16:50:31.147: W/System.err(19007):    at android.os.Handler.handleCallback(Handler.java:587)
07-20 16:50:31.147: W/System.err(19007):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-20 16:50:31.147: W/System.err(19007):    at android.os.Looper.loop(Looper.java:150)
07-20 16:50:31.147: W/System.err(19007):    at android.app.ActivityThread.main(ActivityThread.java:4293)
07-20 16:50:31.147: W/System.err(19007):    at java.lang.reflect.Method.invokeNative(Native Method)
07-20 16:50:31.147: W/System.err(19007):    at java.lang.reflect.Method.invoke(Method.java:507)
07-20 16:50:31.147: W/System.err(19007):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-20 16:50:31.147: W/System.err(19007):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-20 16:50:31.147: W/System.err(19007):    at dalvik.system.NativeStart.main(Native Method)

and I also add the permission into the Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xenos.study.httpclienttest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".HttpClient"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The whole code is here :

public class HttpClientTest extends Activity {


private HttpClient httpClient ;
private HttpGet httpGet ;
private HttpResponse httpResponse ;
private HttpEntity httpEntity ;

private BufferedReader br ;
private Button link ;
private EditText show ;

private String path = "http://74.125.71.104";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    link = (Button) findViewById(R.id.link);
    show = (EditText) findViewById(R.id.show);


    httpClient = new DefaultHttpClient () ;
    httpGet = new HttpGet(path);



    link.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                httpResponse = httpClient.execute(httpGet);
                httpEntity = httpResponse.getEntity();
                br = new BufferedReader(new InputStreamReader(httpEntity.getContent()));

                String str = null ;
                while ((str = br.readLine())!=null) {

                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

    }
  }

It makes me gloomy , thanks for your help.

2

3 Answers 3

19

UPDATE (from comments) You must add

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

To your manifest

Original response

add <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> this to your manifest and check.

Sign up to request clarification or add additional context in comments.

1 Comment

No. He'd rather use <uses-permission android:name="android.permission.INTERNET"/>
0

Plese check your class name because

class name is

public class HttpClientTest extends Activity {

But your activity in Manifest.xml is

android:name=".HttpClient"

I think you essay change activity name is "HttpClientTest" . Because i'm test your code complete.

1 Comment

yes , it makes another problem,but it's unimportant .After I clean the project , it does work . thx for your help .
0

SocketException: It can happen in various situations. Either the server-side closed the connection, or the client-side closed the connection.

Comments

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.