繁体   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