2015-01-13 27 views
0

我有這樣的球衣webResource代碼:爲什麼我的球衣ClientResponse不是空的,但可用返回0?

@Override 
    public int Foo(long uId, String secretKey, long tileId) { 
     int answer = 0; 
     String ans; 
     try { 
      ClientResponse response = webResource 
        // .path("multi-get") 
        .queryParam("reqtype", "tileBatch") 
        .queryParam("protocol", "1") 
        .queryParam("sessionid", String.valueOf(uId)) 
        .queryParam("cookie", String.valueOf(secretKey)) 
        .queryParam("num", "1") 
        .queryParam("t0", String.valueOf(tileId)) 
        .queryParam("v0", "0") 
        .get(ClientResponse.class); 

      if (response.getStatus() != 200) { 
       throw new RuntimeException("Failed : HTTP error code : " 
         + response.getStatus()); 
      } 
      String output = response.getEntity(String.class); 
      answer = response.getEntityInputStream().available(); 

      byte[] byteArray = output.getBytes("UTF-8"); 
      ans = new String(byteArray, "UTF-8"); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return answer; 
    } 
} 

我看String output = response.getEntity(String.class);不爲空,但在answer = response.getEntityInputStream().available();然後answer == 0怎麼來的?

如果我想解析從二進制數據的2個第一個字節到一個整數 我該怎麼做? (int) byteArray[0]

例如00000000-00010000

編輯

我試過這段代碼:

InputStream entityInputStream = response.getEntityInputStream(); 
      answer = entityInputStream.available(); 
      String output = response.getEntity(String.class); 

      byte[] byteArray = new byte[2]; 
      // entityInputStream.reset(); 

      entityInputStream.read(byteArray); 

      String s = new String(byteArray); 

,但尚未byteArray == {0,0}即使output不爲空。

output == .... 
�*WZDF04x��:w|������6[�!�M���@HH �� �����TQ�W�""[email protected]�Z $(���ұ=��[��� ��d�s�n6K�������{�99��{����$qE48" 

是我的方法是否正確?

+0

一旦你閱讀'getEntity'流變空。該流被讀取以轉換爲字符串。同樣的流不能從兩次讀取 –

+0

謝謝。請發表一個答案,我會投票。順便說一句,如果我讀了兩個字節我怎麼能解析它爲int?例如'00000000-00000010'到4? –

+0

請參閱我的更新 –

回答

1

我看到String output = response.getEntity(String.class);不是空的,但answer = response.getEntityInputStream().available();然後answer == 0怎麼回事?

當您執行response.readEntity(String.class)時,正在讀取輸入流並清除輸入流。因此,下次嘗試檢索輸入流將返回一個空的流。

如果我想將2個第一個字節從二進制數據解析爲一個整數,我該怎麼做?

你可以簡單地包裹InputStreamDataInputStream,並使用DataInputStream.readInt()。然後你可以讀取輸入流的其餘部分,用response.readEntity(String.class);。你似乎試圖讀取字符串,然後然後 int,這不符合你的上述聲明。

測試

@Path("/string") 
public class StringResource { 
    @GET 
    public StreamingOutput getString() { 
     return new StreamingOutput(){ 
      @Override 
      public void write(OutputStream out) 
        throws IOException, WebApplicationException { 
       DataOutputStream outStream = new DataOutputStream(out); 
       outStream.writeInt(1234567); 
       outStream.writeUTF("Hello World"); 
      } 
     }; 
    } 
} 

@Test 
public void testMyResource() throws Exception { 
    ClientConfig config = new DefaultClientConfig(); 
    Client client = Client.create(config); 
    WebResource wr = client 
      .resource("http://localhost:8080/api/string"); 
    ClientResponse response = wr.get(ClientResponse.class); 

    InputStream is = response.getEntityInputStream(); 
    DataInputStream dis = new DataInputStream(is); 
    int num = dis.readInt(); 
    System.out.println(num);  
    System.out.println(response.getEntity(String.class)); 
    response.close(); 
} 

打印出1234567然後Hello World

相關問題