Blame view

app/src/main/java/com/example/junsang/simsimi/MainActivity.java 4.4 KB
e12cc46b8   junsang   basic android app
1
  package com.example.junsang.simsimi;
153d373d0   junsang   can print nearest...
2
  import android.annotation.TargetApi;
a7ab6b18f   junsang   UI modified, read...
3
  import android.os.AsyncTask;
153d373d0   junsang   can print nearest...
4
  import android.os.Build;
a7ab6b18f   junsang   UI modified, read...
5
  import android.support.v7.app.ActionBarActivity;
e12cc46b8   junsang   basic android app
6
  import android.os.Bundle;
153d373d0   junsang   can print nearest...
7
  import android.util.Log;
e12cc46b8   junsang   basic android app
8
9
  import android.view.Menu;
  import android.view.MenuItem;
a7ab6b18f   junsang   UI modified, read...
10
  import android.widget.TextView;
e12cc46b8   junsang   basic android app
11

153d373d0   junsang   can print nearest...
12

a7ab6b18f   junsang   UI modified, read...
13
14
  import org.json.JSONArray;
  import org.json.JSONException;
6d661f0e7   junsang   print pm 2.5 in s...
15

153d373d0   junsang   can print nearest...
16
  import org.json.JSONObject;
a7ab6b18f   junsang   UI modified, read...
17
18
19
20
21
22
23
24
25
26
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  
  public class MainActivity extends ActionBarActivity {
153d373d0   junsang   can print nearest...
27
28
29
      private static final String LOG_TAG = "junsang";
  
      int flag = 0; //url flag
e12cc46b8   junsang   basic android app
30

6d661f0e7   junsang   print pm 2.5 in s...
31
      //
153d373d0   junsang   can print nearest...
32
      public String parse_pm(String str) {
6d661f0e7   junsang   print pm 2.5 in s...
33
34
35
36
37
          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;
      }
153d373d0   junsang   can print nearest...
38
39
40
41
42
43
      private static final String URL = "http://jp3.aqicn.org/aqicn/services/geolocate/?autolocate&n=1";
      String nearestPos = "";
      String results;
      TextView pm_tv;
  
      @TargetApi(Build.VERSION_CODES.M)
e12cc46b8   junsang   basic android app
44
45
46
47
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
a7ab6b18f   junsang   UI modified, read...
48
49
50
51
  
          pm_tv = (TextView) findViewById(R.id.pm_tv);
  
          new DownloadPageTask().execute(URL);
e12cc46b8   junsang   basic android app
52
53
54
55
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
e12cc46b8   junsang   basic android app
56
57
58
59
60
61
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
e12cc46b8   junsang   basic android app
62
          int id = item.getItemId();
e12cc46b8   junsang   basic android app
63
64
65
66
67
68
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
a7ab6b18f   junsang   UI modified, read...
69

153d373d0   junsang   can print nearest...
70
       private class DownloadPageTask extends AsyncTask<String, Void, String> {
a7ab6b18f   junsang   UI modified, read...
71
72
73
74
75
76
77
78
79
80
81
82
          @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;
153d373d0   junsang   can print nearest...
83
              String nearestUrl = "";
a7ab6b18f   junsang   UI modified, read...
84

153d373d0   junsang   can print nearest...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
              JSONArray jArr = null;
  
              if(flag==0) {
  
                  try {
                      jArr = new JSONArray(result);
  
                      for (int i = 0; i < jArr.length(); i++) {
                          JSONObject curl = jArr.getJSONObject(i);
                          nearestPos = curl.getString("name");
                          nearestUrl = curl.getString("curl");
                      }
  
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
                  System.out.println(nearestPos);
                  System.out.println(nearestUrl);
                  flag++;
                  new DownloadPageTask().execute(nearestUrl);
a7ab6b18f   junsang   UI modified, read...
105
              }
153d373d0   junsang   can print nearest...
106
107
              else {
              results = result;
a7ab6b18f   junsang   UI modified, read...
108

153d373d0   junsang   can print nearest...
109
              String pm25 ="";
6d661f0e7   junsang   print pm 2.5 in s...
110

153d373d0   junsang   can print nearest...
111
              pm25 = parse_pm(result);
6d661f0e7   junsang   print pm 2.5 in s...
112

153d373d0   junsang   can print nearest...
113
114
                  System.out.println(nearestPos);
                  pm_tv.setText("지금 " + nearestPos + "의 초미세먼지 농도는 " + pm25 + "㎍/㎥야~");
6d661f0e7   junsang   print pm 2.5 in s...
115

153d373d0   junsang   can print nearest...
116
117
                  flag =0;
              }
a7ab6b18f   junsang   UI modified, read...
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
          }
      }
  
      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();
a7ab6b18f   junsang   UI modified, read...
146
147
148
149
150
151
152
153
154
155
156
157
              } else {
                  conn.disconnect();
              }
          } finally {
              if (is != null) {
                  is.close();
              }
          }
  
          return str;
      }
  }