MainActivity.java 4.23 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
package com.example.junsang.simsimi;

import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
//import org.w3c.dom.Document;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;


public class MainActivity extends ActionBarActivity {

private static final String URL = "http://aqicn.org/city/seoul";
String results;
TextView pm_tv;

//
public String parse_pm(String str){
String parse_1 = str.split("<td id=\'cur_pm25\' class=\'tdcur\' style=\'font-weight:bold;font-size:11px;\' align=center>")[1];
String parse_2 = parse_1.split("</td>")[0];

return parse_2;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pm_tv = (TextView) findViewById(R.id.pm_tv);

new DownloadPageTask().execute(URL);
}

@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);
}

private class DownloadPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
return getHtmlFromHttp(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page.";
}
}

@Override
protected void onPostExecute(String result) {
results = result;

// 지금 result : html
// parsing -> JsonArray
String jsonStr = "hi";
try {
JSONArray a = new JSONArray(jsonStr);
} catch (JSONException e) {
e.printStackTrace();
}

jsonStr = parse_pm(result);
/*
Document doc = Jsoup.parse(result);
Elements rows = doc.select("td.line-content ");


for (Element row : rows) {
Iterator<Element> iterElem = row.getElementsByTag("div").iterator();
StringBuilder builder = new StringBuilder();
System.out.println(builder);
builder.append(iterElem.next());
jsonStr= jsonStr+builder.toString();
}*/
// String testStr = (?:"<td id='cur_pm25' class='tdcur' style='font-weight:bold;font-size:11px;' align=center>") result;

pm_tv.setText("지금 서울의 초미세먼지 농도는 "+jsonStr+"㎍/㎥야~");
}
}

private String getHtmlFromHttp(String myUrl) throws IOException {
InputStream is = null;
String str = "";

try {
URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
conn.setDoInput(true);

conn.connect();
int resCode = conn.getResponseCode();

if (resCode == HttpURLConnection.HTTP_OK) {
StringBuffer sb = new StringBuffer();

is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
str = sb.toString();


} else {
conn.disconnect();
}
} finally {
if (is != null) {
is.close();
}
}

return str;
}
}