Android应用中的互联网权限问题?

时间:2015-08-18 18:48:46

标签: android multithreading android-internet

应用名称为:ThreadingNoThreading

有缺陷的类名:threading.java

我的是一个简单的程序。这个特殊的类有两个按钮。一到吐司和另一个从互联网下载图像并在图像视图中设置它。使用AsyncTask进行线程化。

当我在MOTO E上运行程序时,它会抛出一个运行时异常,声明它需要INTERNET权限,我已经提供了。似乎我错过了成功运行此应用程序的一部分难题。

注意:通过连接到WIFI的笔记本电脑调试此应用程序,因此我在笔记本电脑上禁用了我的LAN卡。 (正如我所读,Android系统将LAN CARD视为主要连接)

如果我遗失任何东西,请帮助我理解。

以下是我的课程: 的 Threading.java

package android.dreamtechprojects.threadingnothreading;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * Created by Sony on 18-08-2015.
 */
public class Threading extends Activity {

    Button threadingToastButton, threadingImageViewButton;
    ImageView threadingImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_for_threading);

        threadingImageView = (ImageView) findViewById(R.id.threadingImageView);
        threadingImageViewButton = (Button) findViewById(R.id.threadingImageViewButton);
        threadingToastButton = (Button) findViewById(R.id.threadingToastButton);

        // Start the Async Task to download the image, when this button is pressed.
        threadingImageViewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DownloadImage().execute("http://img3.wikia.nocookie.net/__cb20120826123355/vssaxtonhale/images/c/c2/Troll-face.png");
            }
        });


        threadingToastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(),"Toast received", Toast.LENGTH_LONG).show();
            }
        });



    }


    // Set a drawable to the image view
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setImage(Drawable drawable) {
        threadingImageView.setBackground(drawable);
    }


    // Inner class to download the image
    public class DownloadImage extends AsyncTask<String, Integer, Drawable> {

        @Override
        protected Drawable doInBackground(String... strings) {
            return downloadImage(strings[0]);
        }


        @Override
        protected void onPostExecute(Drawable drawable) {
            setImage(drawable);
        }

        // Logic to download the image
        private Drawable downloadImage(String _url) {

            // Prepare to download the imate
            URL url;
            InputStream inputStream;
            BufferedInputStream bufferedInputStream;

            try {
                url = new URL(_url);
                inputStream = url.openStream();

                // Read the input stream
                bufferedInputStream = new BufferedInputStream(inputStream);

                // Convert the input stream into a bitmap
                Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);

                //Close the streams
                if (inputStream != null) {
                    inputStream.close();
                }

                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }

                return new BitmapDrawable(bitmap);
            } catch (IOException e) {
                e.printStackTrace();

            }
            return null;

        }
    }
}

以下是我的 AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.dreamtechprojects.threadingnothreading">

    <uses-permission android:name="ANDROID.PERMISSION.INTERNET" />
    <uses-permission android:name="ANDROID.PERMISSION.ACCESS_WIFI_STATE" />
    <uses-permission android:name="ANDROID.PERMISSION.CHANGE_WIFI_STATE" />
    <uses-permission android:name="ANDROID.PERMISSION.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">


        <activity android:name=".StartActivity">
            <intent-filter>
                <action android:name="ANDROID.INTENT.ACTION.MAIN" />
                <category android:name="ANDROID.INTENT.CATEGORY.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Threading">

        </activity>

        <activity android:name=".NoThreading">

        </activity>

    </application>

</manifest>

以下是我的 layout_for_threading.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/threadingImageView"
        android:contentDescription="@string/ImageViewContentDescriptior"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/threadingImageViewButton"
        android:text="@string/ImageViewButton"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/threadingToastButton"
        android:text="@string/ToastButton"/>

</LinearLayout>

以下是运行时异常

08-19 00:00:42.134  14371-14563/android.dreamtechprojects.threadingnothreading E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: android.dreamtechprojects.threadingnothreading, PID: 14371
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:304)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:199)
            at java.net.URL.openStream(URL.java:470)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.downloadImage(Threading.java:92)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.doInBackground(Threading.java:73)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.doInBackground(Threading.java:69)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
            at libcore.io.Posix.android_getaddrinfo(Native Method)
            at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:199)
            at java.net.URL.openStream(URL.java:470)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.downloadImage(Threading.java:92)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.doInBackground(Threading.java:73)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.doInBackground(Threading.java:69)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
            at libcore.io.Posix.android_getaddrinfo(Native Method)
            at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:199)
            at java.net.URL.openStream(URL.java:470)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.downloadImage(Threading.java:92)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.doInBackground(Threading.java:73)
            at android.dreamtechprojects.threadingnothreading.Threading$DownloadImage.doInBackground(Threading.java:69)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:6)

include/configs/espresso.h 替换为所有ANDROID.PERMISSION元素中的android.permission。只有最后一段(例如<uses-permission>)全部上限。

最有可能的是,由于this Android Studio bugINTERNET元素被错误地大写,这应该在Android Studio 1.4中修复(当前只发布在金丝雀频道中)。