有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java读取在线存储的文本文件

我正在尝试读取一个文本文件,该文件在线存储在我的Android Studio应用程序中。 我尝试了一些代码,这些代码似乎会在我的应用程序一打开就崩溃。以下是我尝试过的代码:

    protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_browser);
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);

    mWebView.setWebViewClient(new WebViewClient());
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    mWebView.getSettings().setAppCacheEnabled(false);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setEnableSmoothTransition(true);
    CookieManager.getInstance().setAcceptCookie(true);
    mWebView.clearCache(true);

    try {
        URL url = new URL("http://urlhere.co.uk/files/testFile.txt");

        HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
        InputStream is = con.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String line;
        String link = null;

        while((line = br.readLine()) != null) {
            link = line;
        }
        mWebView.loadUrl("file:///" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + link);
        br.close();
    } catch (MalformedURLException e) {
        System.out.println();
    } catch (IOException e) {
        System.out.print("test test");
    }

}

作为一点解释。文本文件包含一个URL,该URL将加载到webview中。这是我认为最好的方式来做的事情,我正试图这样做

更新 我使用了logcat,得到了一些错误,我认为是由以下原因造成的:

java.lang.ClassCastException: com.安卓.okhttp.internal.huc.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

有人对我的应用程序为什么会马上崩溃有什么建议吗

提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    谢谢你的回复。真不敢相信这是这么简单的解决办法。通过更改为http而不是https对象,它消除了更新1中显示的第一个错误

    然而,这导致了安卓系统的崩溃。操作系统。NetworkOnMainThreadException错误,我认为这是因为我没有使用异步。我将把这个问题留给将来遇到这个问题的任何人

    再次感谢所有的回复

  2. # 2 楼答案

    您需要在后台线程中访问网络。一个AsyncTask对于执行此操作和之后在UI线程上访问结果都是有益的。大概是这样的:

    try {
        final URL url = new URL("http://urlhere.co.uk/files/testFile.txt");
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {
                try {
                    URLConnection con = url.openConnection();
                    InputStream is = con.getInputStream();
    
                    BufferedReader br;
                    try {
                        br = new BufferedReader(new InputStreamReader(is));
                        String line;
                        if ((line = br.readLine()) != null) {
                            return line;
                        }
                    } finally {
                        br.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
    
            @Override
            protected void onPostExecute(String link) {
                mWebView.loadUrl("file:///" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + link);            
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    

    (同样,使用URLConnection(或HttpURLConnection)和http。对HttpsURLConnection的强制转换与https一起工作,但看起来您已经发现了。)