安卓:HTTP客户端

标签 android http android-manifest

我正在尝试来自 svn.apache.org 的 http-cleint 教程。运行应用程序时,我在控制台中收到以下错误。

[2010-04-30 09:26:36 - HalloAndroid] ActivityManager: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.org.example/.HalloAndroid } from null (pid=-1, uid=-1) requires android.permission.INTERNET

我在 AndroidManifest.xml 中添加了 android.permission.INTERNET。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.org.example"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HalloAndroid"
                  android:label="@string/app_name" android:permission="android.permission.INTERNET">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

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

</manifest>

HalloAndroid.java中的java代码如下

 HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget2 = new HttpGet("http://google.com/");
        HttpResponse response2 = null;
        try {
            response2 = httpclient.execute(httpget2);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    HttpEntity entity = response2.getEntity();
    if (entity != null) {
        long len = entity.getContentLength();
        if (len != -1 && len < 2048) {
            try {
                    Log.d(TAG, EntityUtils.toString(entity));
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        } else {
            // Stream content out
        }

非常感谢任何帮助。

最佳答案

问题解决了。 AndroidManifest.xml 文件中的这一行导致了问题。

android:permission="android.permission.INTERNET"

关于安卓:HTTP客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2742236/

相关文章:

android - 使用 Android Gradle 更改版本代码

android - 防止 Android 屏幕在 Activity 运行时变暗

android 录音机启动无效状态 4

android: 方法 Split 可以返回空值吗?

javascript - 服务器轮询的替代方案?

javascript - Node JS 发送后无法设置 header

java - 如何使用java以编程方式更改 vector 绘图的大小

android - 为什么我不能在构建脚本中使用 gradle task connectedDebugAndroidTest?

http - HTTP header 中的 SP 字符?

java - 如何在android studio中更改apk文件名