gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:54:09 24 4
gpt4 key购买 nike

我是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 instanceby showing a progress bar in the user interface.

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

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

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

来自developer doc

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com