簡體   English   中英

為什么我的球衣ClientResponse不為空但可用卻返回0?

[英]why is my jersey ClientResponse not empty and yet available returns 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怎么來的?

如果我想將二進制數據的前兩個字節解析為整數,該怎么辦? (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);

但是即使output不為空, byteArray == {0,0}

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

我的方式正確嗎?

我看到String output = response.getEntity(String.class); 不為空,但answer = response.getEntityInputStream().available(); 那么answer == 0怎么來的?

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

如果我想將二進制數據的前兩個字節解析為整數,該怎么辦?

您可以將InputStream封裝在DataInputStream ,然后使用DataInputStream.readInt() 然后,您可以使用response.readEntity(String.class);讀取其余的輸入流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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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