我是java新手,正在尝试从Web中将文本文件读入变量,但是我收到文本文件的网址,而不是内容,只是不知道可能是什么问题。在将txt文件读入变量时获取文本文件的url而不是java中的内容
在那里我试图读取文件中的类:
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);
}
}
在那里我调用类的活动:
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。也许问题出在Activity的调用线上? new readtextfile(description).onPostExecute(“http://www.example.com/description1.txt”); –
对不起。我没有注意到..而不是直接调用onPostExecute尝试execute()方法。像新的readtextfile(描述).execute(); –
好吧,我已经尝试了执行方法,但现在我得到空的结果。该死的。 –