簡體   English   中英

在Java中嘗試塊后為什么不能打印到控制台?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM