改装+;RxJava2在解析JSON正文之前访问它
我使用的是Reformation+RxJava2,我想在Reformation中通过GSON库解析json主体之前访问json主体,下面是我的代码:改装+;RxJava2在解析JSON正文之前访问它,java,android,retrofit,retrofit2,rx-java2,Java,Android,Retrofit,Retrofit2,Rx Java2,我使用的是Reformation+RxJava2,我想在Reformation中通过GSON库解析json主体之前访问json主体,下面是我的代码: trafficApi .trafficControlDetails() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(application.defaultSubscribeSchedul
trafficApi
.trafficControlDetails()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(application.defaultSubscribeScheduler())
.subscribeWith(new DisposableObserver<TrafficDetailItem>()
{
@Override
public void onNext(@io.reactivex.annotations.NonNull TrafficDetailItem trafficDetailItemJsonResponse)
{
SplashViewModel.this.trafficControlDetails = trafficDetailItemJsonResponse;
}
@Override
public void onError(@io.reactivex.annotations.NonNull Throwable e)
{
retryButtonVisibility.set(View.VISIBLE);
errorMessageVisibility.set(View.VISIBLE);
errorMessage.set(context.getString(R.string.connection_error_message));
}
@Override
public void onComplete()
{
retryButtonVisibility.set(View.INVISIBLE);
errorMessageVisibility.set(View.INVISIBLE);
saveData(trafficControlDetails);
}
});
trafficApi
.trafficControlDetails()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(application.defaultSubscribeScheduler())
.subscribowith(新可处置观察员)
{
@凌驾
public void onNext(@io.reactivex.annotations.NonNull TrafficDetailItem trafficDetailItemJsonResponse)
{
SplashViewModel.this.trafficControlDetails=trafficDetailItemJsonResponse;
}
@凌驾
public void onError(@io.reactivex.annotations.NonNull Throwable e)
{
retryButtonVisibility.set(View.VISIBLE);
errorMessageVisibility.set(View.VISIBLE);
errorMessage.set(context.getString(R.string.connection\u error\u message));
}
@凌驾
未完成的公共空间()
{
retryButtonVisibility.set(视图不可见);
errorMessageVisibility.set(视图不可见);
保存数据(流量控制详细信息);
}
});
正如您所看到的,我在我的observer中接收解析的POJO,但我不知道如何获得纯JSON数据。我给您留下一个示例,说明如何请求数据并解析JSON内容,然后从该内容创建一个可观察的对象
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://dummy.restapiexample.com/api/v1/employees"))
.timeout(Duration.ofSeconds(4))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JsonObject jsonResponse = new JsonParser().parse(response.body()).getAsJsonObject();
JsonArray data = jsonResponse.getAsJsonArray("data");
Observable.fromIterable(data)
.filter(s -> {
JsonObject currentElement = s.getAsJsonObject();
Integer age = currentElement.get("employee_age").getAsInt();
return age >= 60;
})
.sorted((x,y) ->
Integer.compare(
x.getAsJsonObject().get("employee_age").getAsInt(),
y.getAsJsonObject().get("employee_age").getAsInt()))
.map(s -> {
JsonObject current = s.getAsJsonObject();
JsonObject result = new JsonObject();
result.addProperty("name", current.get("employee_name").getAsString());
result.addProperty("age", current.get("employee_age").getAsInt());
result.addProperty("id", current.get("id").getAsInt());
result.addProperty("salary", current.get("employee_salary").getAsInt());
return result;
}).doOnError(s -> System.out.println("Map went wrong"))
.subscribe(s -> System.out.println(s), s -> System.out.println("error with " + s));
} catch (Exception e) {
System.out.println("error" +e.toString());
}
HttpClient=HttpClient.newHttpClient();
HttpRequest请求=HttpRequest.newBuilder()
.GET()
.uri(uri.create(“http://dummy.restapiexample.com/api/v1/employees"))
.超时(持续时间秒(4))
.build();
试一试{
HttpResponse response=client.send(请求,HttpResponse.BodyHandlers.ofString());
JsonObject jsonResponse=新建JsonParser().parse(response.body()).getAsJsonObject();
JsonArray data=jsonResponse.getAsJsonArray(“数据”);
可观测的。可观测的(数据)
.过滤器(s->{
JsonObject currentElement=s.getAsJsonObject();
整数年龄=currentElement.get(“雇员年龄”).getAsInt();
返回年龄>=60岁;
})
.已排序((x,y)->
整数。比较(
x、 getAsJsonObject().get(“员工年龄”).getAsInt(),
y、 getAsJsonObject().get(“员工年龄”).getAsInt())
.map(s->{
JsonObject current=s.getAsJsonObject();
JsonObject结果=新建JsonObject();
result.addProperty(“name”,current.get(“employee_name”).getAsString();
result.addProperty(“age”,current.get(“employee_age”).getAsInt();
result.addProperty(“id”,current.get(“id”).getAsInt();
result.addProperty(“salary”,current.get(“employee_salary”).getAsInt();
返回结果;
}).doon错误(s->System.out.println(“地图出错”))
.subscribe(s->System.out.println,s->System.out.println(“带“+s”的错误));
}捕获(例外e){
System.out.println(“错误”+e.toString());
}
我给您留下了一个示例,如何请求数据并解析Json内容,然后从该内容创建一个可观察的对象
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://dummy.restapiexample.com/api/v1/employees"))
.timeout(Duration.ofSeconds(4))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JsonObject jsonResponse = new JsonParser().parse(response.body()).getAsJsonObject();
JsonArray data = jsonResponse.getAsJsonArray("data");
Observable.fromIterable(data)
.filter(s -> {
JsonObject currentElement = s.getAsJsonObject();
Integer age = currentElement.get("employee_age").getAsInt();
return age >= 60;
})
.sorted((x,y) ->
Integer.compare(
x.getAsJsonObject().get("employee_age").getAsInt(),
y.getAsJsonObject().get("employee_age").getAsInt()))
.map(s -> {
JsonObject current = s.getAsJsonObject();
JsonObject result = new JsonObject();
result.addProperty("name", current.get("employee_name").getAsString());
result.addProperty("age", current.get("employee_age").getAsInt());
result.addProperty("id", current.get("id").getAsInt());
result.addProperty("salary", current.get("employee_salary").getAsInt());
return result;
}).doOnError(s -> System.out.println("Map went wrong"))
.subscribe(s -> System.out.println(s), s -> System.out.println("error with " + s));
} catch (Exception e) {
System.out.println("error" +e.toString());
}
HttpClient=HttpClient.newHttpClient();
HttpRequest请求=HttpRequest.newBuilder()
.GET()
.uri(uri.create(“http://dummy.restapiexample.com/api/v1/employees"))
.超时(持续时间秒(4))
.build();
试一试{
HttpResponse response=client.send(请求,HttpResponse.BodyHandlers.ofString());
JsonObject jsonResponse=新建JsonParser().parse(response.body()).getAsJsonObject();
JsonArray data=jsonResponse.getAsJsonArray(“数据”);
可观测的。可观测的(数据)
.过滤器(s->{
JsonObject currentElement=s.getAsJsonObject();
整数年龄=currentElement.get(“雇员年龄”).getAsInt();
返回年龄>=60岁;
})
.已排序((x,y)->
整数。比较(
x、 getAsJsonObject().get(“员工年龄”).getAsInt(),
y、 getAsJsonObject().get(“员工年龄”).getAsInt())
.map(s->{
JsonObject current=s.getAsJsonObject();
JsonObject结果=新建JsonObject();
result.addProperty(“name”,current.get(“employee_name”).getAsString();
result.addProperty(“age”,current.get(“employee_age”).getAsInt();
result.addProperty(“id”,current.get(“id”).getAsInt();
result.addProperty(“salary”,current.get(“employee_salary”).getAsInt();
返回结果;
}).doon错误(s->System.out.println(“地图出错”))
.subscribe(s->System.out.println,s->System.out.println(“带“+s”的错误));
}捕获(例外e){
System.out.println(“错误”+e.toString());
}
okhttp拦截器?你能告诉我们更多你想要实现的目标吗?如果所有这些都失败了,您可以让改型根本不解析响应体,只订阅原始JSON。然后在你的