Skip to content

Instantly share code, notes, and snippets.

@LenKIM
Last active October 8, 2018 10:24
Show Gist options
  • Save LenKIM/a667fee9e6e0a4c1c3d200b6d87b6533 to your computer and use it in GitHub Desktop.
Save LenKIM/a667fee9e6e0a4c1c3d200b6d87b6533 to your computer and use it in GitHub Desktop.
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.sun.media.jfxmediaimpl.MediaDisposer;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.Test;
import java.io.IOException;
public class HttpPractice {
private static final int TARGET_RATING_SCORE = 4;
@Test
public void getJsonByHTTP() {
String url = "https://yts.ag/api/v2/list_movies.json?limit=50";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
try {
Response response = client.newCall(request).execute();
String body = response.body().string();
JsonElement jsonParser = new JsonParser().parse(body);
JsonElement a = jsonParser.getAsJsonObject().get("data");
JsonArray abc = a.getAsJsonObject().get("movies").getAsJsonArray();
// System.out.println(abc.get(0).getAsJsonObject().get("rating"));
Observable.fromIterable(abc)
.filter((abcd) -> abcd.getAsJsonObject().get("rating").getAsInt() >= TARGET_RATING_SCORE)
.map((qq) -> qq.getAsJsonObject().get("title").toString())
.doOnNext(System.out::println)
.subscribe();
Observable.fromIterable(abc)
.map((qq) -> qq.getAsJsonObject().get("year").toString())
.groupBy((Function<String, Object>) s -> (Integer) Integer.parseInt(s))
.flatMapSingle(Observable::toList)
.subscribe(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment