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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With