我在服务器上有一个文本文件(目前在本地服务器上由WAMP在c:/wamp/www/android/sample.txt中)和一个具有3个通过WiFi读取数据的活动的android应用程序。 第一个获取地址(在本地主机上使用10.0.2.2/android/sample.txt)并转到activity2。在activity2中,我有一个进入activity3的按钮。
代码是第三项活动:
private InputStream OpenHttpConnection(String urlString) throws Exception {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection)) {
throw new IOException("NOT an HTTP Connection!");
}
try {
HttpURLConnection httpCon = (HttpURLConnection) conn;
httpCon.setAllowUserInteraction(false);
httpCon.setInstanceFollowRedirects(true);
httpCon.setRequestMethod("GET");
httpCon.connect();
response = httpCon.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpCon.getInputStream();
Log.d("myerr", response + "");
}
} catch (Exception e) {
Log.d("myerr2", e.getLocalizedMessage());
throw new IOException("Error Connection!");
}
return in;
}
private String DownloadText(String URL) {
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (Exception e) {
Log.d("myerr", e.getLocalizedMessage());
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer)) > 0) {
String readString = String
.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (Exception e) {
Log.d("myerr", e.getLocalizedMessage());
return "";
}
return str;
}
private class DownloadTextTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
return DownloadText(urls[0]);
}
protected void onPostExecute(String result) {
Global.readedDataFromFile=result;
//Toast.makeText(DrawRhActivity.this,"Result: "+Global.readedDataFromFile, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw_rh);
String user_address = Global.ip_address;
new DownloadTextTask().execute(user_address);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Value: " + Global.readedDataFromFile);
}
我还在Global.java中定义了一些全局变量。 这是我的问题: 第三个活动第一次不显示textview上的数据。但当我回到第二个活动并按下按钮我的数据加载。 为什么AsyncTask第一次没有显示结果以及如何解决这个问题?
感谢您的关注。
答案 0 :(得分:1)
tv.setText("Value: " + Global.readedDataFromFile);
在onPostExecute中写下这一行
protected void onPostExecute(String result) {
Global.readedDataFromFile=result;
//Toast.makeText(DrawRhActivity.this,"Result: "+Global.readedDataFromFile, Toast.LENGTH_LONG).show();
tv.setText("Value: " + Global.readedDataFromFile);
}
答案 1 :(得分:1)
<强>解决方案:强>
将tv.setText("Value: " + Global.readedDataFromFile);
放入onPostExecute
方法。
<强>阐释:强>
AsyncTask
在单独的线程上运行,而不是在您的UI线程上运行。
所以当它被执行时Global.readedDataFromFile
可能是空的。当执行完成时,它进入onPostExecute
方法,现在Global.readedDataFromFile
存储了一些值。
<强>问题:强>
您在致电new DownloadTextTask().execute(user_address);
后立即设置文字
因此,AsyncTask
尚未完成且Global.readedDataFromFile
为空。
<强>参考:强> AsyncTask
我希望它会有所帮助!!
答案 2 :(得分:0)
问题在于您的onCreate
功能:
String user_address = Global.ip_address;
new DownloadTextTask().execute(user_address);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Value: " + Global.readedDataFromFile);
首先启动任务,然后设置视图,但任务未完成。
您必须将具有任务结果的视图设置为任务的onPostExecute
。
答案 3 :(得分:0)
保留您的DownloadTextTask&amp; 在您的活动中尝试此代码
DownloadTextTask textTask = new DownloadTextTask();
textTask.execute(user_address);
String strDownloaded = "";
try {
strDownloaded = textTask.get();
} catch (Exception e) {
Log.e("DownloadTextTask", "Error: " + e.getMessage());
}