从响应中提取JSON作为ResponseEntityProxy {[Content-Type:application/json; charset = UTF-8,Chunked:true]} [英] Extracting JSON from response as ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}

查看:896
本文介绍了从响应中提取JSON作为ResponseEntityProxy {[Content-Type:application/json; charset = UTF-8,Chunked:true]}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将文件上传到url,并且仅收到 ResponseEntityProxy {[Content-Type:application/json; charset = UTF-8,Chunked:true]}} .据我了解,那里有一个JSON字符串作为响应,我需要以某种方式将其提取.
这是到目前为止我尝试执行的代码(这是在我将文件上传到url的方法中):

I am trying to upload file to an url, and have received instead of regular JSON string response, only ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}. As I understood, there is JSON string as a response in there and I need to extract it somehow.
Here is my code what I have tried so far to do (this is in the method where I am uploading file to an url):

 public String uploadDocument() {
    
    String responseMsg="empty";
     
    try(CloseableHttpClient client = HttpClients.createDefault()){
    
        HttpPost httpPost = new HttpPost(SupportUtil.WHATSAPP_PUSH_BASEURL+"/media/upload/");
        httpPost.addHeader("Content-type", "application/octet-stream");
        httpPost.addHeader("Authorization", "Bearer " + SupportUtil.WHATSAPP_PUSH_KEY);
        
        File file=null;
        try {
            file = ResourceUtils.getFile("/home/ubuntu/DanFagin.pdf");
        } catch (FileNotFoundException e2) {
            
            e2.printStackTrace();
        }
        
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("file",file,ContentType.APPLICATION_OCTET_STREAM,"DanFagin.pdf");
        
        org.apache.http.HttpEntity multipart = builder.build();
        httpPost.setEntity(multipart);

        try (CloseableHttpResponse response = client.execute(httpPost)) {
            
            System.out.println(response.getEntity().toString());
            responseMsg= EntityUtils.toString(response.getEntity(),"UTF-8");
            System.out.println(responseMsg);
            
        } catch (ClientProtocolException e) {
            
            e.printStackTrace();
        } catch (IOException e) {
            
            e.printStackTrace();
        }
    
        System.out.println("Response is closed");
        System.out.println(responseMsg);
        
     } catch (IOException e1) {
        
        e1.printStackTrace();
    }
       
    System.out.println("Client is closed");
    return responseMsg;
    
}

因此,在上面的代码中,我已经构建了HttpResponse的实体(代码较早),然后尝试使用 EntityUtils.toString(entity)方法获取该实体的内容.但是当我使用该方法时,出现以下错误:

So here above I've built the entity of HttpResponse (earlier in code), and then trying to use EntityUtils.toString(entity) method for getting content of the entity. But when I use the method, I get the following error:

org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
    at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:263)
    at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:222)
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:183)
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.Reader.read(Reader.java:140)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:227)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:270)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:290)
    at com.wingsure.WSWhatsapp.repository.RestTemplateImpl.uploadDocument(RestTemplateImpl.java:564)
    at com.wingsure.WSWhatsapp.service.RestTemplateService.uploadDocument(RestTemplateService.java:49)
    at com.wingsure.WSWhatsapp.repository.WebHookRepository.getWebHookDetails(WebHookRepository.java:60)
    at com.wingsure.WSWhatsapp.service.ProductService.getWebHookDetails(ProductService.java:84)
    at com.wingsure.WSWhatsapp.controller.ProductController.getWebHookDetails(ProductController.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
etc

第564行是 EntityUtils.toString()方法所在的位置,没有它,我不会收到任何错误.另一方面,没有找到其他方法可以获取实体的内容,即.JSON字符串,而不使用 EntityUtils.toString()方法.
有人知道这里可能有什么问题吗?非常感谢您的帮助和建议.在每种情况下,谢谢您.

Line 564 is where the EntityUtils.toString() method is, and without it, I don't receive any error. On the other side, didn't find any other way how can I obtain the content of the entity, ie. the JSON string, without using EntityUtils.toString() method.
Does someone perhaps know what can be issue here? I would greatly appreciate any help and suggestion.. Thank you in each case.

推荐答案

尽量不要关闭连接

httpclient.close();
response.close();

让框架为您关闭它.我从来不记得之前关闭过它.只是我的建议.

Let the framework close it for you. I never remember closing this before. Just my suggestion.

这篇关于从响应中提取JSON作为ResponseEntityProxy {[Content-Type:application/json; charset = UTF-8,Chunked:true]}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆