vistaでandroid studio その11

# 概要
vistaでandroid studio 1.0.1やってみた。
練習問題やってみた。

# 練習問題
qiita apiを叩いて、jsonをパースして、listviewに表示せよ。

# 写真

![device-2022-09-03-065018.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/6c25fd12-a8f0-a44a-0222-c11f2b2bfa83.png)


# レイアウト

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:paddingLeft="@dimen/activity_horizontal_margin"
	android:paddingRight="@dimen/activity_horizontal_margin"
	android:paddingTop="@dimen/activity_vertical_margin"
	android:paddingBottom="@dimen/activity_vertical_margin"
	tools:context=".MainActivity">

	<ListView
		android:id="@+id/list"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:background="#33cccc"
		/>

</RelativeLayout>
```


# サンプルコード

```
package com.ohisamallc.ohiapp154.ohiapp154;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends ActionBarActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		run();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	public void run() {
		new AsyncTask<Void, Void, String>() {
			ListView  list = (ListView) findViewById(R.id.list);
			final ArrayList<String> data = new ArrayList<>();
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_single_choice, data);
			@Override
			protected String doInBackground(Void... voids) {
				String dis = "";
				String urlStr = "https://qiita.com/api/v2/users/ohisama@github/items";
				String result = null;
				HttpsURLConnection con;
				URL url = null;
				BufferedReader reader = null;
				try
				{
					Log.e("ohi", "ok0");
					url = new URL(urlStr);
					con = (HttpsURLConnection) url.openConnection();
					Log.e("ohi", "ok1");
					con.setRequestMethod("GET");
					Log.e("ohi", "ok2");
					final int status = con.getResponseCode();
					Log.e("ohi", "ok3");
					if (status == HttpsURLConnection.HTTP_OK)
					{
						Log.e("ohi", "ok4");
						StringBuilder stringBuilder = new StringBuilder();
						reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
						String inputLine;
						while ((inputLine = reader.readLine()) != null)
						{
							stringBuilder.append(inputLine);
						}
						dis = stringBuilder.toString();
					}
					else
					{
						result = String.valueOf(status);
						Log.e("ohi", "err1");
					}
				}
				catch (Exception e)
				{
					Log.e("ohi", e.toString());
					dis = e.toString();
				}
				return dis;
			}
			@Override
			protected void onPostExecute(String result) {
				try
				{
					JSONArray jsonArray = new JSONArray(result);
					for (int i = 0; i < jsonArray.length(); i++)
					{
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						data.add(jsonObject.getString("title"));
					}
					list.setAdapter(adapter);
					list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
							CharSequence msg = ((TextView) view).getText();
							Toast.makeText(MainActivity.this, String.format("選択したのは%s", msg.toString()), Toast.LENGTH_SHORT).show();
						}
					});
				}
				catch (JSONException e)
				{
					e.printStackTrace();
				}
			}
		}.execute();
	}
}

```
以上。