Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android + JSON-RPC

operandoOS
September 16, 2016

Android + JSON-RPC

Android + JSON-RPC

shibuya.apk #10
http://shibuya-apk.connpass.com/event/39433/

operandoOS

September 16, 2016
Tweet

More Decks by operandoOS

Other Decks in Technology

Transcript

  1. RPC • gRPC • http://www.grpc.io/ • Go Conference 2016 SpringͰgRPCͷݱঢ়ʹ͍ͭͯ

    ൃද͖ͯ͠·ͨ͠ • http://tech.mercari.com/entry/ 2016/04/27/145227
  2. JSON-RPC Request [ { "id": 1, "jsonrpc": "2.0", "method": "shibuya.apk",

    "params": { ... } }, { "id": 2, "jsonrpc": "2.0", "method": "shinobu.apk", "params": { ... } } ]
  3. JSON-RPC Response [ { "id": 1, "jsonrpc": "2.0", "result": {

    ... } }, { "id": 2, "jsonrpc": "2.0", "result": { ... } } ]
  4. JSON-RPC Request [ { "id": 1, "jsonrpc": "2.0", "method": "GetTimeline",

    "params": { ... } }, { "id": 2, "jsonrpc": "2.0", "method": "GetBanner", "params": { ... } } ]
  5. JSON-RPC Response [ { "id": 1, "jsonrpc": "2.0", "result": {

    ... } }, { "id": 2, "jsonrpc": "2.0", "result": { ... } } ]
  6. OkHttp࢖ͬͯॻ͍ͯΈΔ Request OkHttpClient client; JSONObject json = new JSONObject(); JsonArray

    params = new JsonArray(); json.put("method","subtract"); params.put(42); params.put(23); json.put("params",params); RequestBody requestBody = RequestBody.create(MEDIA_TYPE, json.toString().getBytes()); Request request = new Request.Builder() .url("https://....") .post(requestBody) .build(); Response response = client.newCall(request).execute();
  7. OkHttp࢖ͬͯॻ͍ͯΈΔ Response Response response = client.newCall(request).execute(); // ... isSuccessfulͷνΣοΫͱ͔... ResponseBody

    value = response.body(); JSONObject j = new JSONObject(value.string()); int result = j.getInt("result");// return 19
  8. ے೑ 2ͭ @Getter public class Pair<F, S> { private final

    F first; private final S second; private Pair(F first, S second) { this.first = first; this.second = second; } public static <F, S> Pair<F, S> create(F first, S second) { return new Pair<>(first, second); } }
  9. ے೑ 3ͭ @Getter public class Triplet<F, S, T> { private

    final F first; private final S second; private final T thread; private Triplet(F first, S second, T thread) { this.first = first; this.second = second; this.thread = thread; } public static <F, S, T> Triplet<F, S, T> create(F first, S second, T thread) { return new Triplet<>(first, second, thread); } }
  10. ෳ਺ͷϦΫΤετΛ࡞Δ // ϦΫΤετͱϨεϙϯεΛΫϥεͱͯ͠ఆٛ // ϦΫΤετ : GetBanner Ϩεϙϯε : GetBanner

    class GetBannerResponse { String url; } // ϦΫΤετͷܕ͕ܾ·Ε͹Ϩεϙϯεͷܕ͕ܾ·Δ = RequestType<Ϩεϙϯεͷܕ> class GetBanner extends RequestType<GetBannerResponse> { @Override public String getMethod() { return "GetBanner"; } @Override protected Class<GetBannerResponse> getResponseType() { return GetBannerResponse.class; } }
  11. ෳ਺ͷϦΫΤετΛ࡞Δ // ಉ͡Α͏ʹผAPIͷϦΫΤετͱϨεϙϯεΛΫϥεͱͯ͠ఆٛ // ϦΫΤετ : GetTimeline Ϩεϙϯε : GetTimelineResponse

    class GetTimelineResponse { List<Item> items; } class GetTimeline extends RequestType<GetTimelineResponse> { private final long timelineId; GetTimeline(long timelineId) { this.timelineId = timelineId; } @Override public String getMethod() { return "GetTimeline"; } @Override protected Class<GetTimelineResponse> getResponseType() { return GetTimelineResponse.class; } }
  12. ࡞ͬͨϦΫΤετΛ·ͱΊͯAPIʹ౤͛Δ GetTimeline getTimeline = new GetTimeline(1); GetBanner getBanner = new

    GetBanner(); RxApiClient rxApiClient = new RxApiClient(); // Observable<Pair<GetBannerResponse, GetTimelineResponse>> ͕ฦͬͯ͘Δ rxApiClient.responseFrom(getTimeline,getBanner) ....
  13. ϦΫΤετͷΫϥε͸JSONʹͳͬͯ HTTP bodyʹઃఆ͞ΕΔ [ { "params": { "timelineId": 1 },

    "method": "GetTimeline", "jsonrpc": "2.0", "id": "1" }, { "params": {}, "method": "GetBanner", "jsonrpc": "2.0", "id": "2" } ]
  14. ෳ਺ͷϨεϙϯε͕ฦͬͯ͘Δ + 
 ϋϯυϦϯάͯ͠ϨεϙϯεΛ௨஌͢Δ // ෳ਺ͷϨεϙϯεTupleʹแΜͰRxʹྲྀ͢ // responseFrom಺ͰTupleʹแΉॲཧΛͯ͠Δ rxApiClient.responseFrom(getTimeline, getBanner)

    .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Pair<GetTimelineResponse, GetBannerResponse>>() { @Override public void call(Pair<GetTimelineResponse, GetBannerResponse> pair) { // Tuple͔ΒϨεϙϯεΛऔΓग़͢ pair.getFirst(); // return GetTimelineResponse pair.getSecond(); // retrun GetBannerResponse } });