Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取内容类型为application/vnd.oracle.adf.resourceitem+的rest服务;json_Java_Web Services_Rest_Http Headers_Oracle Adf - Fatal编程技术网

Java 读取内容类型为application/vnd.oracle.adf.resourceitem+的rest服务;json

Java 读取内容类型为application/vnd.oracle.adf.resourceitem+的rest服务;json,java,web-services,rest,http-headers,oracle-adf,Java,Web Services,Rest,Http Headers,Oracle Adf,我有一个内容类型为application/vnd.oracle.adf.resourceitem+json的Web服务 通过点击此服务获得的响应的HttpEntity如下所示 ResponseEntityProxy{[Content-Type: application/vnd.oracle.adf.resourceitem+json,Content-Length: 3,Chunked: false]} 当我试图将这个HttpEntity转换成字符串时,它会给我一个空白字符串{} 下面是我试图将

我有一个内容类型为application/vnd.oracle.adf.resourceitem+json的Web服务

通过点击此服务获得的响应的HttpEntity如下所示

ResponseEntityProxy{[Content-Type: application/vnd.oracle.adf.resourceitem+json,Content-Length: 3,Chunked: false]}
当我试图将这个HttpEntity转换成字符串时,它会给我一个空白字符串
{}

下面是我试图将
HttpEntity
转换为
字符串的方法

一,

String strResponse=EntityUtils.toString(response.getEntity())

二,

三,

All返回字符串->
{}

谁能告诉我我做错了什么


这是因为内容类型吗?

上面的代码仍然使用空JSON对象给出相同的响应。所以我修改并编写了下面的代码。这台似乎运行得很好

URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

con.setDoOutput(true);
con.setRequestMethod("POST");
con.addRequestProperty("Authorization", getAuthToken());
con.addRequestProperty("Content-Type", "application/vnd.oracle.adf.resourceitem+json;charset=utf-8");

String input = String.format("{\"%s\":\"%s\",\"%s\":\"%s\"}", field, value, field2, value2);

System.out.println(input);
OutputStream outputStream = con.getOutputStream();
outputStream.write(input.getBytes());
outputStream.flush();

con.connect();
System.out.println(con.getResponseCode());
// Uncompressing gzip content encoding
GZIPInputStream gzip = new GZIPInputStream(con.getInputStream());

StringBuffer szBuffer = new StringBuffer();

byte tByte[] = new byte[1024];

while (true) {
    int iLength = gzip.read(tByte, 0, 1024);

    if (iLength < 0) {
        break;
    }

    szBuffer.append(new String(tByte, 0, iLength));
}
con.disconnect();

returnString = szBuffer.toString();
以防有人面临同样的问题。让我分享我所面临的挑战,以及我是如何纠正这些挑战的

以上代码适用于所有内容类型/方法。可用于任何类型(GET、POST、PUT、DELETE)。 根据我的要求,我有一个POST网站服务

内容编码→gzip

内容类型→application/vnd.oracle.adf.resourceitem+json

挑战:我能够得到正确的响应代码,但是我得到了垃圾字符作为我的响应字符串

解决方案:这是因为输出被压缩为
gzip
格式,需要解压缩

上面还提到了解压缩
gzip内容编码的代码

希望它能帮助未来的用户

response.getEntity().writeTo(new FileOutputStream(new File("C:\\Users\\harshita.sethi\\Documents\\Chabot\\post.txt")));
URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

con.setDoOutput(true);
con.setRequestMethod("POST");
con.addRequestProperty("Authorization", getAuthToken());
con.addRequestProperty("Content-Type", "application/vnd.oracle.adf.resourceitem+json;charset=utf-8");

String input = String.format("{\"%s\":\"%s\",\"%s\":\"%s\"}", field, value, field2, value2);

System.out.println(input);
OutputStream outputStream = con.getOutputStream();
outputStream.write(input.getBytes());
outputStream.flush();

con.connect();
System.out.println(con.getResponseCode());
// Uncompressing gzip content encoding
GZIPInputStream gzip = new GZIPInputStream(con.getInputStream());

StringBuffer szBuffer = new StringBuffer();

byte tByte[] = new byte[1024];

while (true) {
    int iLength = gzip.read(tByte, 0, 1024);

    if (iLength < 0) {
        break;
    }

    szBuffer.append(new String(tByte, 0, iLength));
}
con.disconnect();

returnString = szBuffer.toString();
private String getAuthToken() {
        String name = user;
        String pwd = this.password;
        String authString = name + ":" + pwd;
        byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
        System.out.println(new String(authEncBytes));
        return "Basic " + new String(authEncBytes);
    }