2015-12-06 87 views
0

我正在創建一個eclipse插件,它從Web of Trust API中接收JSON並將其打印到控制檯中。我能夠打印整個JSON,但是當我嘗試獲取特定信息時,它不會顯示在插件控制檯中。如何使用MessageConsoleStream在插件中打印JSON到控制檯

try { 

JSONObject json = readJsonFromUrl("http://api.mywot.com/0.4/public_link_json2?hosts=google.com/&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); 
      JSONObject msg = (JSONObject) json.getJSONObject("google.com").getJSONObject("categories"); 
      String[] parts = msg.toString().split(","); 
      String[] parts2 = parts[1].split(":"); 


      // System.out.println("Score:" + parts2[0]); 
     //System.out.println("Score:" + parts2[0]); 
     //print result 
     MessageConsoleStream out = ConsoleManager.getMessageConsoleStream("Console"); 
     out.println("Score:" + msg.toString()); 

    } 
catch (Exception e) { 

} 

    return null; 
} 





private static String readAll(Reader rd) throws IOException { 
    StringBuilder sb = new StringBuilder(); 
    int cp; 
    while ((cp = rd.read()) != -1) { 
     sb.append((char) cp); 
    } 
    return sb.toString(); 
    } 

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { 
     InputStream is = new URL(url).openStream(); 
     try { 
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); 
      String jsonText = readAll(rd); 
      JSONObject json = new JSONObject(jsonText); 
      return json; 
     } finally { 
      is.close(); 
     } 
} 

ConsoleManager類:

public class ConsoleManager { 

public static MessageConsole findConsole(String name) { 
     ConsolePlugin plugin = ConsolePlugin.getDefault(); 
     IConsoleManager conMan = plugin.getConsoleManager(); 
     IConsole[] existing = conMan.getConsoles(); 
     for (int i = 0; i < existing.length; i++) 
      if (name.equals(existing[i].getName())) return (MessageConsole) existing[i]; 
     //no console found, so create a new one 
     MessageConsole myConsole = new MessageConsole(name, null); 
     conMan.addConsoles(new IConsole[]{myConsole}); 
     return myConsole; 
    } 

    public static MessageConsoleStream getMessageConsoleStream(String name) { 
     MessageConsole myConsole = findConsole(name); 
     MessageConsoleStream out = myConsole.newMessageStream(); 
     return out; 
    } 
} 

所以我試圖打印從JSON存儲在parts2的價值,但是當我嘗試將它打印到控制檯插件,它不顯示任何東西。沒有錯誤,所以我想我錯過了一些東西。

任何幫助,非常感謝。

回答

0

您的控制檯是當前顯示的控制檯嗎?

您可能想要撥打IConsoleManager.showConsoleView(console)來顯示您的控制檯。

+0

我這麼認爲,我以前在控制檯上顯示過數值。在eclipse的例子中,顯示控制檯,它只是沒有輸出。 – IHZachR

相關問題