概要
vistaでandroid studio 1.0.1やってみた。
練習問題やってみた。
練習問題
qiita apiを叩け。
サンプルコード
public void run(View view) {
TextView tv = (TextView) findViewById(R.id.chage);
String dis = "";
String urlStr = "https://qiita.com/api/v2/users/ohisama@github/items";
String result = null;
HttpURLConnection con;
URL url = null;
BufferedReader reader = null;
try
{
Log.e("ohi", "ok0");
url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection();
Log.e("ohi", "ok1");
con.setRequestMethod("GET");
Log.e("ohi", "ok2");
final int status = con.getResponseCode();
Log.e("ohi", "ok3");
if (status == HttpURLConnection.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());
}
tv.setText(dis);
}
エラー android.os.NetworkOnMainThreadException
どうやら Android 3.0 以降は、Main スレッド( UI スレッド)上でネットワーク通信を行うとこのエラーが発生するようです。
このエラーを回避するには、ネットワーク通信を別スレッドで実行する必要があるようです。
言ってよ。
サンプルコード
public void run(View view) {
new AsyncTask<Void, Void, String>() {
TextView tv = (TextView) findViewById(R.id.chage);
@Override
protected String doInBackground(Void... voids) {
String dis = "";
String urlStr = "https://qiita.com/api/v2/users/ohisama@github/items";
String result = null;
HttpURLConnection con;
URL url = null;
BufferedReader reader = null;
try
{
Log.e("ohi", "ok0");
url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection();
Log.e("ohi", "ok1");
con.setRequestMethod("GET");
Log.e("ohi", "ok2");
final int status = con.getResponseCode();
Log.e("ohi", "ok3");
if (status == HttpURLConnection.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) {
tv.setText(result);
}
}.execute();
}
}
エラー javax.net.ssl.SSLException
HTTPSで通信する
サンプルコード
public void run(View view) {
new AsyncTask<Void, Void, String>() {
TextView tv = (TextView) findViewById(R.id.chage);
@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) {
tv.setText(result);
}
}.execute();
}
}
写真
以上。