gpt4 book ai didi

java - 为什么我的 Jersey ClientResponse 不为空,但可用返回 0?

转载 作者:行者123 更新时间:2023-12-01 12:02:07 33 4
gpt4 key购买 nike

我有这个 Jersey 网络资源代码:

   @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();答案==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);

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

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

我的方法正确吗?

最佳答案

I see String output = response.getEntity(String.class); is not empty and yet in answer = response.getEntityInputStream().available(); then answer == 0 how come?

当您执行response.readEntity(String.class)时,将读取输入流并清除输入流。因此,您下次尝试检索输入流将返回一个空流。

If I want to parse the 2 first bytes from binary data into an integer how can I do this?

您可以简单地将InputStream 包装在DataInputStream 中,并使用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

关于java - 为什么我的 Jersey ClientResponse 不为空,但可用返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27918979/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com