我试图在android中的AsyncTask类中从Web读取文本文件,然后将结果传递给静态变量,然后将其加载到另一个类的TextView中,但是我是得到空的结果,无法弄清楚原因。显然,我在代码中弄乱了一些东西......
有人可以评论这个吗?提前谢谢!
package com.example.cuccandroid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.AsyncTask;
public class readtextfile extends AsyncTask<String, Integer, Void>{
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL("http://example.com/example.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
while ((line = in.readLine()) != null) {
//get lines
Kezdoh.descriptiontext=line;
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate() {
//called when the background task makes any progress
}
protected void onPreExecute() {
//called before doInBackground() is started
}
protected void onPostExecute() {
//called after doInBackground() has finished
}
}
我的班级,我试图加载
package com.example.cuccandroid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import com.example.cluppandroid.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Kezdoh extends Fragment{
TextView description;
static String descriptiontext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.kezdoh, container, false);
//GetDescription();
description = ((TextView)android.findViewById(R.id.description1));
new readtextfile().execute("http://example.com/example.txt");
description.setText(descriptiontext);
return android;
}
}
答案 0 :(得分:3)
您必须记住AsyncTask在不同的时间在不同的线程中执行。
在你的情况下,description.setText(descriptiontext);首先执行。
一种好的方法是阻止使用descriptiontext的静态引用。
在这种情况下,您可以做的是向readtextfile添加构造函数并传递TextView实例。 onPostExecute()方法将在读取字符串内容的doInBackground()之后执行。 随后,应在onPostExecute()方法中访问任何与UI相关的方法。
我稍微修改了你的readtextfile类,如下所示:
public class readtextfile extends AsyncTask<String, Integer, String>{
private TextView textViewToSet;
public readtextfile(TextView descriptionTextView){
this.textViewToSet = descriptionTextView;
}
@Override
protected String doInBackground(String... params) {
String result = "";
try {
URL url = new URL("http://example.com/example.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
while ((line = in.readLine()) != null) {
//get lines
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.textViewToSet.setText(result);
}
}
接下来你需要做的就是打电话
new readtextfile().execute("http://example.com/example.txt");
并从onCreateView方法中删除此行
description.setText(descriptiontext);
答案 1 :(得分:0)
我已更改了您的asynctask
课程,请在TextView
被调用后检查并在onpostExecute()
中设置super()
。请检查以下代码。
public class readtextfile extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
StringBuilder response = new StringBuilder();
try {
URL url = new URL(
"http://example.com/example.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
String line = null;
while ((line = in.readLine()) != null) {
// get lines
response.append(line);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
protected void onProgressUpdate() {
// called when the background task makes any progress
}
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.show();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
Log.d("tag", "Response is " + result.toString());
//set your Textview here
description.setText(result.toString());
}
}
您的主要班级
package com.example.cuccandroid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import com.example.cluppandroid.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Kezdoh extends Fragment{
TextView description;
static String descriptiontext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.kezdoh, container, false);
//GetDescription();
description = ((TextView)android.findViewById(R.id.description1));
new readtextfile().execute("http://example.com/example.txt");
return android;
}
}