説明
AndroidでOkHttpの実装します。
OkHttp URL:http://square.github.io/okhttp/
サンプルで使用するAPIは、
HeartRails Geo APIの都道府県を取得するAPIを使用します。
HeartRails Geo API URL:
http://geoapi.heartrails.com/api.html
都道府県取得(XML用) URL:
http://geoapi.heartrails.com/api/xml?method=getPrefectures
ちなみにJsonだとわかりづらいのでXMLですが、返却される値です。

実装説明
・GradleでOkHttpライブラリを設定します。
compile 'com.squareup.okhttp3:okhttp:3.8.1'
・インターネット通信を行うためパーミッションの設定をしておきます。
<uses-permission android:name="android.permission.INTERNET"/>
・OkHttpClienntの実態を生成します。
OkHttpClient client = new OkHttpClient();
・Requestの設定を行います。
Request request = new Request.Builder()
.url(url)
.build();
・Requestを送ります。
onFailure:ネットワークが繋がっていない時などの失敗時に通ります。
onResponse:正常にデータが返ってきた時に通ります。
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
....
}
@Override
public void onResponse(Call call, Response response) throws IOException {
....
}
});
サンプル
activity_main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</android.support.constraint.ConstraintLayout>
list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:gravity="center"
android:orientation="vertical"
android:text="TEXT"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"></TextView>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private OkHttpClient client;
private ArrayList prefectures;
private final String URL =
"http://geoapi.heartrails.com/api/json?method=getPrefectures";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL)
.build();
final ListView listView =
(ListView) findViewById(R.id.list_view);
prefectures = new ArrayList<>();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// 取得が成功したかどうか
if(response.isSuccessful()) {
String string = response.body().string();
try {
JSONObject json = new JSONObject(string);
JSONObject responce =
json.getJSONObject("response");
JSONArray prefecturesObject =
responce.getJSONArray("prefecture");
for(int i=0; i<prefecturesObject.length(); ++i) {
prefectures.add(prefecturesObject.getString(i));
}
// UIスレッドでリストの更新を行う
Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
ArrayAdapter arrayAdapter =
new ArrayAdapter(MainActivity.this,
R.layout.list_item,
prefectures);
listView.setAdapter(arrayAdapter);
}
});
}catch (JSONException jsonEx) {
jsonEx.printStackTrace();
}
} else {
throw new IOException();
}
}
});
}
}
完成したスクリーンショットです。


都道府県が取得出来ていますね。
以上です。