我目前困惑。我在onPostExecute()中写入的一些行不会被调用。这里是我的AsyncTask代码:为什么有些行在我的onPostExecute()中没有被调用?
public class GuestInfoTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
StringBuilder stringBuilder = new StringBuilder();
try
{
URL url = new URL(params[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
int charRead;
char[] charBuffer = new char[500]; // download 500 characters at a time
while(true)
{
charRead = inputStreamReader.read(charBuffer);
if(charRead <= 0)
break;
stringBuilder.append(String.valueOf(charBuffer), 0, charRead);
}
return stringBuilder.toString();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s != null)
{
// THIS DOES NOT GET CALLED!
System.out.println("Successfully retrieved guest info with response: " + s);
// TODO: parse the data here
// THIS DOES NOT GET CALLED ALSO!
System.out.println("TEST: " + s);
// BUT THIS GETS CALLED
// alert dialog the response here
new AlertDialog.Builder(GuestInfoActivity.this)
.setTitle("Guest Info")
.setMessage("Message: " + s)
.setPositiveButton("OK", null)
.show();
}
else
{
System.out.println("Failed to retrieve guest info! Double check your server address and/or network connection!");
new AlertDialog.Builder(GuestInfoActivity.this)
.setTitle("Error")
.setMessage("Failed to retrieve guest info! Double check your server address and/or network connection!")
.setPositiveButton("OK", null)
.show();
}
// disable the progressbar after
mProgressDialog.dismiss();
}
}
我有一个button
是挖掘时,执行上述AsyncTask
。现在任务完成后,我成功地在屏幕上看到了AlertDialog
,但没有看到printLines
。我也试过Log.i()
但结果相同。
这是为什么?我的目的是否有问题?为什么它是值得的,我在Android Studio 1.51和模拟器API 19
编辑:我想我已经缩小了罪魁祸首。如果我键入Log.d(“tag”,“test”),则会在日志中成功显示。但是如果我输入Log.d(“tag”,s),其中s
是onPostExecute的结果,我在日志中看不到它。这似乎有一个问题,输出onPostExecute结果字符串?
可能您的logcat筛选级别设置为仅显示更高级别的消息。 – laalto
清理建立您的项目,并再次在您的设备上运行。 –
将'System.out.println()'改为'Log.d(“tag”,“message”);'如果执行再次检查 – ELITE