java - 将txt文件读入变量时获取文本文件的url而不是java中的内容

标签 java android

我是java新手,我试图将网络上的文本文件读取到变量中,但我得到的是文本文件的url,而不是内容,只是无法弄清楚可能是什么问题所在。

我尝试读取文件的类:

public class readtextfile extends AsyncTask<String, Integer, String>{

private TextView description;
public readtextfile(TextView descriptiontext){
    this.description = descriptiontext;
    }

@Override
protected String doInBackground(String... params) {
    URL url = null;
    String result ="";
    try {

        url = new URL("http://example.com/description1.txt");       
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
        result+=line;
        }
        in.close();

        } 
        catch (MalformedURLException e) {e.printStackTrace();} 
        catch (IOException e) {e.printStackTrace();}
    return result;
}

 protected void onProgressUpdate() {
    //called when the background task makes any progress
 }

  protected void onPreExecute() {
     //called before doInBackground() is started
 }

@Override
 protected void onPostExecute(String result) {
    this.description.setText(result); 
 }
  }

我调用该类的 Activity :

public class PhotosActivity extends Activity {

    TextView description;
    String descriptiontext;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photos_layout);

        description = ((TextView)findViewById(R.id.description1));

        new readtextfile(description).execute();
        }   

    }

最佳答案

尝试 url.openConnection 并使用连接对象来获取 inputStream。更新后的方法就像

protected String doInBackground(String... params) {
    URL url = null;
    String result = "";
    try {
        url = new URL("http://www.example.com/description1.txt");
        URLConnection connection = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        in.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

根据您的评论进行更新。

您无需调用 postExecute 方法。如果您调用 postExecute,它只会执行该方法。 doInBackground 不会被执行。相反,您应该使用执行方法。就像java thread.start()方法调用重写的run()方法一样。

异步任务执行时,任务会经历4个步骤:

onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

来自developer doc

关于java - 将txt文件读入变量时获取文本文件的url而不是java中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23404678/

相关文章:

java - Jar 捆绑程序无法从 jar 文件自动获取主类。您能描述一下整个过程吗?

java - 为什么 Google fit API 不断返回空数据集?

android - 使用 Android Jetpack Compose 构建首选项屏幕

Android Camera2 Renderscript 过热问题

java - 尝试通过rest访问jpa数据时获取 "No mapping found for HTTP request with URI [/system/people]..."

java - 如何在 Java 中处理 128 位小端乘法而不求助于 BigInteger

java - 使用XSLT输出多个文件

android - 在 Android 中支持背景颜色的圆角 ImageView?

java - Android 上数组中的黑名单字符串

java - JUnit 测试返回 null 和 false