我正在尝试使用CloseableHttpAsyncClient
从端点读取,将字符串编组为Object(使用javax.json),然后将对象上的数组转换为其各个组件:
CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().setDefaultCredentialsProvider(provider).build();
client.start();
Observable<ObservableHttpResponse> observable = ObservableHttp.createRequest(HttpAsyncMethods.createGet(uri), client)
.toObservable();
Observable<JsonArray> shareable = observable.flatMap(response -> response.getContent().map(bb -> {
String stringVal = new String(bb);
StringReader reader = new StringReader(stringVal);
JsonObject jobj = Json.createReader(reader).readObject();
return jobj.getJsonArray("elements");
})).share();
我需要获取Json数组,然后过滤数组的对象:
Observable<JsonObject> firstStream = shareable.filter(item -> item.getString("type").equals("TYPE_1"));
Observable<JsonObject> secondStream = shareable.filter(item -> item.getString("type").equals("TYPE_2"));
Observable<JsonObject> thirdStream = shareable.filter(item -> item.getString("type").equals("TYPE_3"));
如何将Observable<JsonArray>
转换为ObservableJsonObject>
?
因为它是异步的,我不能使用forEach来创建某种数组来缓冲数据。
所以看看使用CloseableHttpAsyncClient可能不是我想要实现的最好的解决方案 - 我今天早上意识到(在万物之中)我正在尝试异步处理数据然后进行异步调用
理想情况下,调用CloseableHttpClient(sync)并将数据传递给Observable进行过滤将是一种更理想的方法(我不需要第一次调用来管理多个http调用)。
CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
StringBuffer result = new StringBuffer();
try {
HttpGet request = new HttpGet(uri);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch(ClientProtocolException cpe) { } catch(IOException ioe) { }
StringReader reader = new StringReader(result.toString());
JsonObject jobj = Json.createReader(reader).readObject();
JsonArray elements = jobj.getJsonArray("elements");
List<JsonObject> objects = elements.getValuesAs(JsonObject.class);
Observable<JsonObject> shareable = Observable.from(objects).share();
Observable<JsonObject> firstStream = shareable.filter(item -> item.getString("type").equals("TYPE_1"));
Observable<JsonObject> secondStream = shareable.filter(item -> item.getString("type").equals("TYPE_2"));
Observable<JsonObject> thirdStream = shareable.filter(item -> item.getString("type").equals("TYPE_3"));
firstStream.subscribe(record -> {
//connect to SOTS/Facebook and store the results
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Json.createWriter(baos).writeObject(record);
System.out.println(baos.toString());
});
secondStream.subscribe(record -> {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Json.createWriter(baos).writeObject(record);
System.out.println(baos.toString());
});
thirdStream.subscribe(record -> {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Json.createWriter(baos).writeObject(record);
System.out.println(baos.toString());
});
答案 0 :(得分:2)
试试这段代码:
String myjson = "{\"elements\": [{\"text\":\"Obj1\"},{\"text\":\"Obj2\"}, {\"text\":\"Obj3\"}]}";
Observable.just(myjson)
.map(jsonStr -> new StringReader(myjson))
.map(reader -> Json.createReader(reader).readObject())
.map(jobj -> jobj.getJsonArray("elements"))
.map(elements -> elements.toArray(new JsonObject[elements.size()]))
.flatMap(jsonObjects -> Observable.from(jsonObjects))
.subscribe(
(jsonObject) -> System.out.println(jsonObject.getString("text")),
throwable -> throwable.printStackTrace(),
() -> System.out.println("On complete"));
结果:
07-22 12:19:22.362 8032-8032 / com.mediamanagment.app I / System.out: Obj1
07-22 12:19:22.362 8032-8032 / com.mediamanagment.app I / System.out:Obj2
07-22 12:19:22.362 8032-8032 / com.mediamanagment.app I / System.out:Obj3
注意:强>
你应该使用这种依赖:
compile 'org.glassfish:javax.json:1.0.4'
取而代之的是:
compile 'javax.json:javax.json-api:1.0'
如果您使用'javax.json:javax.json-api:1.0'
,您将获得javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
步骤:
.map(reader -> Json.createReader(reader).readObject())
请使用'org.glassfish:javax.json:1.0.4'
<强>更新强> 而且,而不是
.flatMap(jsonObjects -> Observable.from(jsonObjects))
您可以使用flatMapIterable( )
:
.flatMapIterable(jsonObjects -> jsonObjects)
答案 1 :(得分:0)
您应该能够使用其他flatMap()
来电,而不是您正在使用的map()
来电。然后使用Observable.create()
发出JsonObject
s
Observable<JsonObject> shareable =
observable
.flatMap(response -> response.getContent()
.flatMap(bb -> {
String stringVal = new String(bb);
StringReader reader = new StringReader(stringVal);
JsonObject jobj = Json.createReader(reader).readObject();
JsonArray elements = jobj.getJsonArray("elements");
return Observable.create(subscriber -> {
for (int i = 0; i < elements.length(); i++) {
subscriber.onNext(elements.getJSONObject(i));
}
subscriber.onCompleted();
});
}))
.share();