OKHttp的ServerSideEvent客户端,具有自定义内容类型

时间:2019-06-12 22:50:13

标签: java okhttp3

我正在尝试为HTTP SSE编写流客户端,在此我们从服务器流json文档。我在客户端使用OKHttpSSE接收文档。我们为我们的API提供了一个自定义的接受标头,当我尝试在OKHttp请求中传递自定义的接受类型时,收到以下错误“在使用事件源时出现[ERROR]异常...”。

"java.lang.IllegalStateException: Invalid content-type: application/vnd.abc.xyz.platform.pqr-ijk+json; version=1;charset=utf-8"

Note: "vnd.abc.xyz.platform.pqr-ijk+json; version=1;charset=utf-8" is my ACCEPT header.

final Request httpRequest =  new Request.Builder()
     .url("https://abcd.xyz.com/stream/myresources/")
     .addHeader("From", "1234")
     .addHeader("Authorization", "myauthtoken")
     .addHeader("Accept", "vnd.abc.xyz.platform.pqr- 
                           ijk+json;version=1;")
     .build();
final OkHttpClient httpClient = createHttpClient();

final EventSource.Factory sseFactory = 
                   EventSources.createFactory(httpClient);
final EventSourceListener eventSourceListener = new 
              EventSourceListener() {
        /**
        * {@inheritDoc}
        */
       @Override
       public void onOpen(final EventSource eventSource, final Response 
        response) {
             logger.info("EventSource opened...");
       }

      /**
       * {@inheritDoc}
       */
      @Override
      public void onEvent(final EventSource eventSource, final String 
          id, final String type, final String data) {
         logger.info("{}: {}", id, data);
      }

     /**
      * {@inheritDoc}
      */
     @Override
     public void onClosed(final EventSource eventSource) {
         logger.info("EventSource closed...");
     }

     /**
       * {@inheritDoc}
       */
      @Override
     public void onFailure(final EventSource eventSource, final 
                      Throwable t, final Response response) {
        logger.error("Exception while consuming event source... 
           [Response: 
                {}]...", response, t);
      } 

     };

     sseFactory.newEventSource(httpRequest, eventSourceListener);

以下是错误信息 java.lang.IllegalStateException:无效的内容类型:“ vnd.abc.xyz.platform.pqr-ijk + json; version = 1     在okhttp3.internal.sse.RealEventSource.processResponse(RealEventSource.kt:60)处[okhttp-sse-4.0.0-RC1.jar :?]     在okhttp3.internal.sse.RealEventSource.onResponse(RealEventSource.kt:46)上[okhttp-sse-4.0.0-RC1.jar :?]     在okhttp3.RealCall $ AsyncCall.run(RealCall.kt:138)[okhttp-4.0.0-RC1.jar :?]     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[?:1.8.0_191]     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)[?:1.8.0_191]     在java.lang.Thread.run(Thread.java:748)[?:1.8.0_191]

0 个答案:

没有答案