gpt4 book ai didi

java - 如何使用 okhttp3 在 Android 中使用 Twitter Streaming API?

转载 作者:行者123 更新时间:2023-11-30 00:46:05 26 4
gpt4 key购买 nike

我想继续消费流响应。到目前为止,这是我的代码。我想我无法长时间保持连接打开。

    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);

}
});

我得到以下异常:

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)

我也试过使用 WebSocketListener。我在 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不是 websocket 调用。参见 https://dev.twitter.com/streaming/overview

您需要将其作为流式常规 HTTP 调用进行处理。

如此处所述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. In that case, prefer to process the body as a stream.

这是一些将响应作为流处理的示例代码

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();
}

关于java - 如何使用 okhttp3 在 Android 中使用 Twitter Streaming API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41788507/

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