2014-02-10 36 views
0

我在我的android應用程序中打開了一個普通的java套接字,我必須使用它來發送請求並接收響應,我正常發送請求但沒有收到服務器的任何響應,我檢查了服務器連接,它是好吧,也是我試圖通過Hercules傾聽和請求被正常發送,服務器通常發送的響應,這是常規的套接字編碼我使用:爲什麼我的套接字沒有收到響應?

public static void xbmc_connect(){ 
    try { 
     xbmc_socket = new Socket(xbmc_address, xbmc_port); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
public static void xbmc_sendingDataToSystem(String data) throws IOException{ 

    xbmc_output = new DataOutputStream(xbmc_socket.getOutputStream()); 
    xbmc_output.writeBytes(data); 

} 

public String xbmc_getServerResponse() throws JSONException{ 

    String responseLine, server_response = null_string; 

     try { 
      xbmc_input = new BufferedReader(new InputStreamReader(
        xbmc_socket.getInputStream())); 
      System.out.println(xbmc_input.toString());// doesn't print anything 
    } catch (IOException e) { 
    } 
    try { 
     while ((responseLine = xbmc_input.readLine()) != null) { 
      server_response = server_response + responseLine ; 
     } 

      return server_response; 

} 

回答

2

簡而言之:xbmc_inputBufferedReader要從BufferedReader中讀取,您必須使用read()readLine()

將BufferedReader的String表示與toString()一樣,沒有什麼意義。 調用toString()不會使BufferedReader打印它可能收到的任何內容。 toString()只需打印BufferedReader對象 - 即對對象的引用。

因此,要打印的內容實際上是接受(如果有的話),你可能會想嘗試:

System.out.println(xbmc_input.readLine()); 
+0

我只是把它進行測試,我不希望它打印任何東西可讀,我只是想它給我任何結果,這讓我知道它收到了任何東西,順便說一句,我讓我用這樣的閱讀線打印它,也沒有提供任何東西 – MRefaat

+1

用toString()打印BufferedReader不會告訴你它是否收到任何東西。你必須調用'read()'或'readLine()'。不過,我現在看到你至少在while循環中做了這個。在上面的catch語句中,執行'e.printStackTrace()'以確保沒有異常被拋出。 –

相關問題