微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有翻新或OkHttp的Twitter API v2流

如何解决带有翻新或OkHttp的Twitter API v2流

我正在尝试将新的Twitter API专门集成到我的android应用中的streaming tweets部分,我正在对我的http调用使用Retrofit。

当我尝试拨打电话以获取流式推文时,它只是挂起,不返回任何内容

这是我的改造电话

<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>Using the `Esc` key to dismiss modal type dialogs</title>
    </head>
    <body>
        <form name='emulator' method='post'>
            <input type='text' name='message' value='banana' />
            <input type='button' name='bttn' value='Submit' />
        </form>
        <!--
        
            Two browsers to test: Chrome & Firefox
            
            In Chrome(Version 84.0.4147.135) a bug seems to prevent normal page operation
            if the `Esc` key is pressed to dismiss the alert prompt. Clicking the button on 
            the `alert` does not yield the same error/problem however and this is not an 
            issue in FF.
            
            There IS page interaction required before the bug/undocumented feature becomes 
            apparent so it seems that the new features designed by google to fight `popup spam`
            ( https://www.zdnet.com/article/google-changes-how-the-escape-key-is-handled-in-chrome-to-fight-popup-ads/ )
            is not relevant.
            
            In Firefox using the `Esc` key no adverse effects are observed and it
            behaves as one might expect.
            
        -->
        <script>
            
            const args={ 'bubbles':true,'cancelable':true };
            const evt=new Event('click',args);
            
            const form=document.forms.emulator;
            const bttn=form.bttn;
            const input=form.message;
            
            /* listen for keypress events and respond with custom event for `enter` key press */
            form.addEventListener('keypress',(e)=>{
                if( e.key === 'Enter' ){
                    e.preventDefault();
                    bttn.dispatchEvent(evt);
                }
            });
            
            /* display a modal type dialog in response to triggered event */
            bttn.addEventListener( 'click',(e) => alert( input.value ) );
            /*
                alternate version - same result
                bttn.addEventListener( 'click',(e) => { alert( input.value ); input.focus(); } );
            */
        </script>
        
        <h2>Process to test/duplicate:</h2>
        <ol>
            <li>Open page in Chrome and Firefox</li>
            <li>Click into the text field to give focus - add some text. ( this stage optional )</li>
            <li>Hit `Enter` on keyboard OR press `Submit` button. An alert will appear displaying the contents of the text field</li>
            <li>Hit `Esc` on keyboard to dismiss the alert. The alert will disappear.</li>
            <li>Click into the textfield again and type - in Chrome focus will not return and button will not respond. In Firefox all OK.</li>
        </ol>
    </body>
</html>

然后我尝试仅用OkHttp进行呼叫,如文档中所示,我获得了成功的响应,但是我不知道如何流传输数据。

我可以通过curl呼叫成功拨打电话,并且看到数据没问题。

如何通过翻新或OkHttp传输数据

更新

使用OkHttp,我可以通过执行此操作获取数据

@Streaming
@GET("tweets/search/stream")
suspend fun getFilteredStream(@Header("Authorization") token:String)

val client: OkHttpClient = OkHttpClient().newBuilder() .build() val request: Request = Request.Builder() .url("https://api.twitter.com/2/tweets/search/stream") .method("GET",null) .build() val response: Response = client.newCall(request).execute() val source = response.body?.source() val buffer = Buffer() while(!source!!.exhausted()){ response.body?.source()?.read(buffer,8192) val data = buffer.readString(Charset.defaultCharset()) } 保存着多个tweet对象的字符串数据表示形式,但是我如何一次读取一个tweet,或解析这样的响应?

解决方法

从文档中,我认为您需要将两个示例结合起来。

https://github.com/square/retrofit/blob/108fe23964b986107aed352ba467cd2007d15208/retrofit/src/main/java/retrofit2/http/Streaming.java

在返回{@link ResponseBody ResponseBody}的方法上处理响应主体,即不将主体转换为{@code byte []}。

示例调用代码

https://github.com/square/retrofit/blob/108fe23964b986107aed352ba467cd2007d15208/retrofit/src/test/java/retrofit2/CallTest.java#L599

我怀疑,取决于您使用的JSON API,您也许可以使用响应正文中的Streaming API。但是,如果不是这样,您可以只在换行符或换行符上进行分割,然后在下一行{处进行分割,然后分别进行解析。抱歉,我不能帮您。

请参见https://en.wikipedia.org/wiki/JSON_streaming#Concatenated_JSON

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。