Android Studio 从服务器读取文本文件(.txt)

w51jfk4q  于 11个月前  发布在  Android
关注(0)|答案(2)|浏览(168)

Android Studio - Java我已经尝试从我的网站读取一个文本文件好几天了。在互联网上找到的示例不起作用。“错误”总是在“catch”中输出(参见下面的代码)。URL的路径是正确的。我用下载测试了它。Internet权限已添加。问题在哪里?

try{

            URL yahoo = new URL("https://www.robl.de/data/HS.txt");
            URLConnection yc = yahoo.openConnection();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            yc.getInputStream()));

            while ((inputLine = in.readLine()) != null)
                System.out.println(inputLine);
            in.close();

    }
    catch ( IOException e ) {
        e.printStackTrace();
        inputLine="error";
    }

 try{
        URL llll = new URL("http://www.robl.de/data/HS.txt");

        URLConnection conLLL = llll.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader( conLLL.getInputStream()));

        while ( ( strLine = br.readLine() ) != null)
            System.out.println(strLine);
        br.close();

    }
    catch(Exception ex)
    {
        ex.printStackTrace();
        strLine="error";
    }

字符串

taor4pac

taor4pac1#

听起来您遇到了NetworkOnMainThreadException异常,该异常在尝试对Android UI线程执行网络操作时发生。Android要求在后台线程上发出网络请求。以下是如何修改代码以使用AsyncTask在后台线程上执行网络操作的方法:

import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class NetworkFileReader extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urlString) {
        StringBuilder content = new StringBuilder();
        try {
            URL url = new URL(urlString[0]);
            URLConnection urlConnection = url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line).append("\n");
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
        return content.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        // This is where you would handle the result of the network operation
        // For example, updating the UI or storing the data
        System.out.println(result);
    }
}

// Usage:
new NetworkFileReader().execute("https://www.robl.de/data/HS.txt");

字符串
确保在Activity或片段中适当地执行此AsyncTask,并在onPostExecute方法中处理结果。
另外,请确保您在AndroidManifest.xml中拥有互联网权限:

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


请注意,AsyncTask在API level 30及更高级别中已被弃用。对于现代Android开发,c enter code here考虑使用Kotlin协程或Java并发实用程序(如Executors)。然而,AsyncTask仍然是快速解决此问题的有效方法,用于教育目的或遗留代码库。

xxe27gdn

xxe27gdn2#

作为参考,请使用 URL#openStream 方法。

URL u = new URI("https://www.robl.de/data/HS.txt").toURL();
try (BufferedReader r = new BufferedReader(new InputStreamReader(u.openStream()))) {
    r.lines().forEach(System.out::println);
}

字符串

相关问题