gpt4 book ai didi

android - 带有改造 2 的流媒体服务器 - 分块

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:25 27 4
gpt4 key购买 nike

我应该从服务器流式传输端点,该端点返回带有传输编码的 json:分块。

我有以下代码,但无法读取响应。我尝试了 responseBody.streamBytes() 并将输入流转换为字符串,但我不能在主线程中执行此操作。我如何阅读回复?

@Streaming
@GET("stream/status")
Observable<ResponseBody> streamStatus();

Observable<ResponseBody> observable = ApiClientHelper.getClient().streamStatus();
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}

@Override
public void onNext(final ResponseBody responseBody) {
//DON'T KNOW HOW TO READ DATA
}

@Override
public void onError(Throwable e) {
}

@Override
public void onComplete() {
}
});

编辑:

使用 CURL 的服务器响应:

*   Trying 192.168.1.3...
* TCP_NODELAY set
* Connected to 192.168.1.3 (192.168.1.3) port 80 (#0)
> GET /stream/meter HTTP/1.1
> Host: 192.168.1.3
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie:sessionId=bf2533346190e1c72b532b9d6ec6a405
>
< HTTP/1.1 200 OK
< Content-Type: text/event-stream
< Cache-Control: no-cache, no-store
< Date: Tue, 17 Oct 2017 12:02:03 GMT
< Transfer-Encoding: chunked
< Connection: Keep-Alive
<
data: {"production":{"ph-a":{"p":-0.817,"q":0.0,"s":47.302,"v":225.697,"i":0.21,"pf":0.0,"f":50.0},"ph-b":{"p":-0.066,"q":-0.0,"s":0.643,"v":3.091,"i":0.206,"pf":0.0,"f":50.0},"ph-c":{"p":-0.195,"q":-0.0,"s":0.943,"v":7.577,"i":0.123,"pf":0.0,"f":50.0}},"net-consumption":{"ph-a":{"p":-0.598,"q":0.0,"s":51.931,"v":225.606,"i":0.231,"pf":0.0,"f":50.0},"ph-b":{"p":-0.088,"q":0.0,"s":0.875,"v":4.585,"i":0.19,"pf":0.0,"f":50.0},"ph-c":{"p":-0.043,"q":0.0,"s":0.16,"v":1.23,"i":0.13,"pf":-1.0,"f":50.0}},"total-consumption":{"ph-a":{"p":-1.415,"q":-0.0,"s":-4.599,"v":225.652,"i":-0.02,"pf":-1.0,"f":50.0},"ph-b":{"p":-0.154,"q":0.0,"s":0.06,"v":3.838,"i":0.016,"pf":-1.0,"f":50.0},"ph-c":{"p":-0.237,"q":0.0,"s":-0.033,"v":4.404,"i":-0.008,"pf":-1.0,"f":50.0}}}

最佳答案

添加 @Streaming 注释导致改造不会将整个文件移动到内存中,而是立即传递传入的字节。这使您能够处理可能大于总可用内存的数据流。但是,如果您尝试在主线程上执行此操作,您将得到一个 android.os.NetworkOnMainThreadException,这就是我假设您得到的。所以问题出在.observeOn(AndroidSchedulers.mainThread)

编辑:公平警告。我没有运行这个。

    observable
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable disposable) {

}

@Override
public void onNext(ResponseBody responseBody) {
InputStream inputStream = responseBody.byteStream();
BufferedReader br = null;
StringBuilder sb = new StringBuilder();

String line;
try {

br = new BufferedReader(new InputStreamReader(inputStream));
while (br.ready()) {
line = br.readLine();
sb.append(line);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Log.d("streamed string", sb.toString()); // replace log with whatever you want to do with it.
}

@Override
public void onError(Throwable throwable) {

}

@Override
public void onComplete() {

}
});

关于android - 带有改造 2 的流媒体服务器 - 分块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46892690/

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