本文介绍了java.net.SocketException异常:权限被拒绝为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建Android应用程序链接www.google.com。但是,我总是得到这样的异常:
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)
和我还添加权限进入的Manifest.xml
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>
整个code是在这里:
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.
推荐答案
添加&LT;使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/&GT;
这对你的清单和检查。
add <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
this to your manifest and check.
这篇关于java.net.SocketException异常:权限被拒绝为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!