AndroidでWebAPIから情報を取得し、画面に表示させるまでの流れをまとめます。
環境:Android Studio 3.6.3
表示させるだけの最低限の実装です。
目標
ボタンを押した際に、書籍のタイトル・出版年月日を表示させます。
情報元には「Google Books API」を用います。
https://developers.google.com/books/
書籍の情報を取得するには、他にも楽天書籍API
https://webservice.rakuten.co.jp/explorer/api/BooksBook/Search/
などがあります。
応用させた自作アプリがこちら
Layout
Layoutは適当です。題名を表示させる”#titleView”、出版年月日を表示させる”#dateView”、ボタンを乱雑に配置します。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<TextView
android:id="@+id/titleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/dateView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titleView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="136dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
今回は、ボタンを押したらAPI通信する、というだけのシンプルな処理のため、全てをMainActivityでまとめて行います。
public class MainActivity extends AppCompatActivity {
//レイアウト要素
Button button;
TextView titleView;
TextView dateView;
//API
private final String API_URL_PREFIX = "www.googleapis.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
titleView = findViewById(R.id.titleView);
dateView = findViewById(R.id.dateView);
descriptionView = findViewById(R.id.descriptionView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//②この中でAPIを叩きます。
}
});
}
//①ここにAPI通信処理を書きます。
}
APIに関する処理を①, ②と省略しました。これを除けば、シンプルな構成です。
では、①API通信処理から見ていきます。
①API通信
class MyAsync extends AsyncTask<String, Void, String> {
private final WeakReference<TextView> titleViewReference;
private final WeakReference<TextView> dateViewReference;
public MyAsync(TextView titletView, TextView dateView) {
titleViewReference = new WeakReference<TextView>(titletView);
dateViewReference = new WeakReference<TextView>(dateView);
}
@Override
protected String doInBackground(String... params) {
final StringBuilder result = new StringBuilder();
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.scheme("https");
uriBuilder.authority(API_URL_PREFIX);
uriBuilder.path("/books/v1/volumes");
uriBuilder.appendQueryParameter("q", "夏目漱石");
final String uriStr = uriBuilder.build().toString();
try {
URL url = new URL(uriStr);
HttpURLConnection con = null;
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect(); //HTTP接続
final InputStream in = con.getInputStream();
final InputStreamReader inReader = new InputStreamReader(in);
final BufferedReader bufReader = new BufferedReader(inReader);
String line = null;
while((line = bufReader.readLine()) != null) {
result.append(line);
}
Log.e("but", result.toString());
bufReader.close();
inReader.close();
in.close();
}
catch(Exception e) { //エラー
Log.e("button", e.getMessage());
}
return result.toString(); //onPostExecuteへreturn
}
@Override
protected void onPostExecute(String result) { //doInBackgroundが終わると呼び出される
try {
JSONObject json = new JSONObject(result);
String items = json.getString("items");
JSONArray itemsArray = new JSONArray(items);
JSONObject bookInfo = itemsArray.getJSONObject(0).getJSONObject("volumeInfo");
String title = bookInfo.getString("title");
String publishedDate = bookInfo.getString("publishedDate");
TextView titleView = titleViewReference.get();
TextView dateView = dateViewReference.get();
titleView.setText(title);
dateView.setText(publishedDate);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
②ボタンが押されたら
asynk = new MyAsync(titleView, dateView);
asynk.execute();
コメント