如何在互联网上读取文件与Android Studio与AsyncTask



我有一个问题,连接到服务器与android工作室读取文件txt。然后,我使用AsyncTask,但问题是,当我打开应用程序并尝试该函数时,它会给我错误。这是代码:

private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        URL url = null;
        try {
            url = new URL("http://tmtsite.altervista.org/Agata/in.txt");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        URLConnection conn = null;
        try {
            conn = url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader rd = null;
        try {
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String line = "";
        StringBuilder builder = new StringBuilder(16384);
        try {
            while ((line = rd.readLine()) != null) {

                builder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }
    @Override
    protected void onPostExecute(String result) {
    }
    @Override
    protected void onPreExecute() {}
    @Override
    protected void onProgressUpdate(Void... values) {}
    }
}

和这是堆栈跟踪:

  06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime: Process: damasoft.agata, PID: 22354
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:309)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:  Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.lookupHostByName(InetAddress.java:464)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:482)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:465)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:447)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:353)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:468)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at damasoft.agata.MainActivity$LongOperation.doInBackground(MainActivity.java:111)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at damasoft.agata.MainActivity$LongOperation.doInBackground(MainActivity.java:95)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at 
android.os.AsyncTask$2.call(AsyncTask.java:295)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:  Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at libcore.io.Posix.android_getaddrinfo(Native Method)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.getAllByName(InetAddress.java:215) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:482) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:465) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:447) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:353) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:468) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at damasoft.agata.MainActivity$LongOperation.doInBackground(MainActivity.java:111) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at damasoft.agata.MainActivity$LongOperation.doInBackground(MainActivity.java:95) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:295) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:  Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at libcore.io.Posix.android_getaddrinfo(Native Method) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.lookupHostByName(InetAddress.java:451) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.net.InetAddress.getAllByName(InetAddress.java:215) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:482) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:465) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:447) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:353) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:468) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at damasoft.agata.MainActivity$LongOperation.doInBackground(MainActivity.java:111) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at damasoft.agata.MainActivity$LongOperation.doInBackground(MainActivity.java:95) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:295) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    06-28 11:12:52.761 22354-22567/damasoft.agata E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 

正如Logcat所说

原因:java.lang.SecurityException: Permission denied (missing .网络允许吗?)

确保在Manifest文件中定义了Internet权限。

使用

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

所以你的manifest.xml文件是这样的

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.project"> 
<uses-permission android:name="android.permission.INTERNET"/>

请显示堆栈跟踪,但可以观察到的一件事是,在获得inputStream之前,您没有调用connect方法。以下是文档的内容:

  1. 通过调用openConnection创建连接对象方法。
  2. 设置参数和一般请求属性是被操纵的。
  3. 建立到远程对象的实际连接;使用connect方法。
  4. 远程对象可用。可以访问远程对象的报头字段和内容。

:

conn.connect();

:

rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

相关内容

  • 没有找到相关文章

最新更新