Я хочу продолжать потреблять ответ потока. Это мой код до сих пор. Я думаю, что я не могу долго поддерживать связь.Как мне использовать Twitter Streaming API в android, используя okhttp3?
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);