我创建了我的Android应用程序来链接“www.google.com”。但我总是得到这样的例外:
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
<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>
整个代码在这里:
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();
}
}
});
}
}
感谢你的帮助,这令我感到沮丧。
答案 0 :(得分:9)
更新(来自评论) 你必须添加
<uses-permission android:name="android.permission.INTERNET"/>
到您的清单
原始回复
将<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
添加到您的清单并检查。
答案 1 :(得分:0)
请检查您的班级名称,因为
班级名称是
公共类HttpClientTest扩展了Activity {
但您在Manifest.xml中的活动是
机器人: “HttpClient的” 名称=
我认为你的论文更改活动名称是“HttpClientTest”。因为我正在测试你的代码。