我目前困惑。我在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