[英]Why can't I print to console after try block in Java?
我在Android應用程序中有以下代碼:
public static HttpResponse dbPost(String handlerUrl, List<NameValuePair> postData) {
HttpClient httpclient = new DefaultHttpClient();
String postUrl = constants.postUrl();
HttpPost httppost = new HttpPost(postUrl);
HttpResponse response = null;
System.out.print("Catch 0");
try {
httppost.setEntity(new UrlEncodedFormEntity(postData));
response = httpclient.execute(httppost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print("Catch 1");
return response;
}
我有一個調用此塊的按鈕。 如果按下按鈕,控制台將打印“ Catch 0”(但不顯示“ Catch 1”)。 如果再次按下按鈕(相同的實例),控制台將打印“ Catch1Catch0”。 有什么問題?
我對Java有點陌生,請耐心等待。
由於使用print
需要致電flush 。
System.out.print("Catch 1");
System.out.flush();
PrintStream
的默認行為是僅在寫入換行符時刷新。 (此行為記錄在write(int)中 。)
如果您使用printLn
而不是print
,則流將自動刷新。 否則,您需要顯式刷新輸出流。
您使用的是print
而不是println
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.