简体   繁体   English

如何使用 okhttp3 在 android 中使用 Twitter Streaming API?

[英]How do I consume Twitter Streaming API in android using okhttp3?

I want to keep consuming the stream response.我想继续使用流响应。 This is my code so far.到目前为止,这是我的代码。 I think I am not able to keep the connection open for long.我想我无法长时间保持连接打开。

    OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer("**********",
                "**********");
        consumer.setTokenWithSecret("***********",
                "*************");

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new SigningInterceptor(consumer))
                .build();

        Request request = new Request.Builder()
                .url("https://stream.twitter.com/1.1/statuses/filter.json?track=twitter")
                .build();

client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {

                final String responseData = response.body().string();
                Log.d("RESPONSE TWEETS" , responseData);

            }
        });

I get the following exception:我收到以下异常:

Callback failure for call to https://stream.twitter.com/...
01-22 13:03:25.238 27785-28965/in.androidlab.twittervisualiser D/OkHttp: java.net.ProtocolException: unexpected end of stream
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okhttp3.internal.http1.Http1Codec$ChunkedSource.read(Http1Codec.java:433)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okio.RealBufferedSource.read(RealBufferedSource.java:45)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okio.RealBufferedSource.exhausted(RealBufferedSource.java:55)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okio.InflaterSource.refill(InflaterSource.java:101)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okio.InflaterSource.read(InflaterSource.java:62)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okio.GzipSource.read(GzipSource.java:80)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okio.Buffer.writeAll(Buffer.java:996)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okio.RealBufferedSource.readString(RealBufferedSource.java:189)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okhttp3.ResponseBody.string(ResponseBody.java:174)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at in.androidlab.twittervisualiser.MainActivity$1.onResponse(MainActivity.java:104)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-22 13:03:25.239 27785-28965/in.androidlab.twittervisualiser D/OkHttp:     at java.lang.Thread.run(Thread.java:818)

I also tried with WebSocketListener.我也尝试过使用 WebSocketListener。 I am not getting any response in onMessage()我在 onMessage() 中没有得到任何响应

 WebSocketListener listener = new WebSocketListener() {
            @Override
            public void onOpen(WebSocket webSocket, Response response) {
                super.onOpen(webSocket, response);
                Log.d("RESPONSE Twitter", response.message());
            }

            @Override
            public void onMessage(WebSocket webSocket, String text) {
                super.onMessage(webSocket, text);
                Log.d("RESPONSE Twitter", text);
            }

            @Override
            public void onMessage(WebSocket webSocket, ByteString bytes) {
                super.onMessage(webSocket, bytes);
            }

            @Override
            public void onClosing(WebSocket webSocket, int code, String reason) {
                super.onClosing(webSocket, code, reason);
            }

            @Override
            public void onClosed(WebSocket webSocket, int code, String reason) {
                super.onClosed(webSocket, code, reason);
            }

            @Override
            public void onFailure(WebSocket webSocket, Throwable t, Response response) {
                super.onFailure(webSocket, t, response);
            }
        };

        WebSocket ws = client.newWebSocket(request, listener);

https://stream.twitter.com/1.1/statuses/filter.json?track=twitter is not a websocket call. https://stream.twitter.com/1.1/statuses/filter.json?track=twitter不是 websocket 调用。 See https://dev.twitter.com/streaming/overview请参阅https://dev.twitter.com/streaming/overview

You will need to process it as a streaming regular HTTP call.您需要将其作为流式常规 HTTP 调用进行处理。

As documented here https://github.com/square/okhttp/wiki/Recipes如此处所述https://github.com/square/okhttp/wiki/Recipes

But if the response body is large (greater than 1 MiB), avoid string() because it will load the entire document into memory.但是,如果响应主体很大(大于 1 MiB),请避免使用 string(),因为它会将整个文档加载到内存中。 In that case, prefer to process the body as a stream.在这种情况下,更喜欢将主体作为流处理。

This is some example code for processing the Response as a stream这是一些用于将响应作为流处理的示例代码

https://github.com/yschimke/oksocial/blob/3757196cde420b9d0fe37cf385b66f4cdafb1ae1/src/main/java/com/baulsupp/oksocial/output/DownloadHandler.java https://github.com/yschimke/oksocial/blob/3757196cde420b9d0fe37cf385b66f4cdafb1ae1/src/main/java/com/baulsupp/oksocial/output/DownloadHandler.java

  @Override public void showOutput(Response response, boolean showHeaders) throws IOException {
    BufferedSource source = response.body().source();

    Sink outputSink = getOutputSink(response);
    try {
      writeToSink(source, outputSink);
    } finally {
      if (!isStdout()) {
        outputSink.close();
      }
    }
  }

  public static void writeToSink(BufferedSource source, Sink out) throws IOException {
    while (!source.exhausted()) {
      out.write(source.buffer(), source.buffer().size());
      out.flush();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM