Last active
November 1, 2015 09:54
-
-
Save suzukaze/cfb17232b2b3aeb9e83f to your computer and use it in GitHub Desktop.
AndroidでシンプルにHTTP通信する方法 ref: http://qiita.com/suzukaze/items/3393cdb8582fd4c98f0b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Observable observable = Observable.create(new Observable.OnSubscribe<Integer>() { | |
@Override | |
public void call(Subscriber<? super Integer> subscriber) { | |
// (1) リクエストをしてレンスポンスをパース | |
subscriber.onCompleted(); // 完了を通知する | |
} | |
}) | |
.subscribeOn(Schedulers.io()); // io用のスレッドで行う設定 | |
ViewObservable.bindView(listView, observable); | |
observable | |
.observeOn(AndroidSchedulers.mainThread()) // メインスレッドで行う設定 | |
.subscribe(new Observer<Integer>() { | |
@Override | |
public void onCompleted() { | |
// (2) 結果を受け取りUI操作をする | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Observable observable = Observable.create(new Observable.OnSubscribe<Integer>() { | |
@Override | |
public void call(Subscriber<? super Integer> subscriber) { | |
EmpitomeBeamService empitomeBeamService = ServiceGenerator.createService( | |
EmpitomeBeamService.class, EmpitomeBeamService.ENDPOINT); // (3) | |
EpitomeBeam epitomeBeam = empitomeBeamService.getBeam(); // (4) | |
for (EpitomeEntry epitomeEntry : epitomeBeam.sources) { | |
titles.add(epitomeEntry.title); | |
} | |
subscriber.onCompleted(); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
observable | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Observer<Integer>() { | |
@Override | |
public void onCompleted() { | |
// (2) 結果を受け取りUI操作をする | |
ArrayAdapter<String> adapter = new ArrayAdapter(finalMainActivity, | |
android.R.layout.simple_expandable_list_item_1, titles); | |
listView.setAdapter(adapter); | |
} | |
@Override | |
public void onError(Throwable e) { | |
} | |
@Override | |
public void onNext(Integer progress) { | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface EmpitomeBeamService { | |
public static final String ENDPOINT = "https://ja.epitomeup.com"; | |
@GET("/feed/beam") | |
EpitomeBeam getBeam(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://ja.epitomeup.com/feed/beam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"sources":[{"id":"176271","scheme":"gists","title":"Samsung と IBM、ビットコインの技術を仮想通貨以外の分野に応用へ","views":347,"epitome_url":"https://ja.epitomeup.com/sources/176271","upstream_url":"http://www.bloomberg.com/news/articles/2015-04-10/samsung-plans-to-take-bitcoin-technology-beyond-virtual-currency","published_at":"2015-04-10T09:47:53.000Z","gists":[{"content":"シリコンバレーを拠点とする Samsung Research America で Bitcoin の応用技術が研究されている"},{"content":"Bitcoin の Blockchain を応用することで、より優れた真正保証手段を作れるのではと考えている"},{"content":"Blockchain は従来のストレージの代替手段としても活用できる"},{"content":"最近は企業の Bitcoin への関心や、Bitcoin 関連スタートアップへの関心が高まってきている"}]},{"id":"142503","scheme":"gists","title":"Facebook の DB エンジニアが語る、InnoDB が優位性を失った理 | |
...省略 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"sources: | |
[{"id":"176271", "scheme":"gists", "title": "title":"Samsung と IBM、ビットコインの技術を仮想通貨以外の分野に応用へ", ..}, | |
{"id":"142503","scheme":"gists","title":"Facebook の DB エンジニアが語る、InnoDB が優位性を失った理由", ...}, | |
... | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EpitomeBeam { | |
@SerializedName("sources") | |
public List<EpitomeEntry> sources; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EpitomeEntry { | |
final static String SCHEME_GISTS = "gists"; | |
@SerializedName("id") | |
public String id; | |
@SerializedName("scheme") | |
public String scheme; | |
@SerializedName("title") | |
public String title; | |
//... 省略 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (EpitomeEntry epitomeEntry : epitomeBeam.sources) { | |
titles.add(epitomeEntry.title); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
subscriber.onCompleted(); // 完了を通知する |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment