Commit 153d373d033945735b113b8518e5513e95530a1a

Authored by junsang
1 parent 6d661f0e7b

can print nearest city's pm2.5

Showing 11 changed files with 83 additions and 37 deletions

.idea/gradle.xml View file @ 153d373
... ... @@ -11,6 +11,7 @@
11 11 <set>
12 12 <option value="$PROJECT_DIR$" />
13 13 <option value="$PROJECT_DIR$/app" />
  14 + <option value="$PROJECT_DIR$/json-simple-1.1.1" />
14 15 <option value="$PROJECT_DIR$/jsoup-1.8.3" />
15 16 </set>
16 17 </option>
.idea/modules.xml View file @ 153d373
... ... @@ -4,6 +4,7 @@
4 4 <modules>
5 5 <module fileurl="file://$PROJECT_DIR$/Simsimi.iml" filepath="$PROJECT_DIR$/Simsimi.iml" />
6 6 <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
  7 + <module fileurl="file://$PROJECT_DIR$/json-simple-1.1.1/json-simple-1.1.1.iml" filepath="$PROJECT_DIR$/json-simple-1.1.1/json-simple-1.1.1.iml" />
7 8 <module fileurl="file://$PROJECT_DIR$/jsoup-1.8.3/jsoup-1.8.3.iml" filepath="$PROJECT_DIR$/jsoup-1.8.3/jsoup-1.8.3.iml" />
8 9 </modules>
9 10 </component>
... ... @@ -98,6 +98,7 @@
98 98 <orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
99 99 <orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
100 100 <orderEntry type="module" module-name="jsoup-1.8.3" exported="" />
  101 + <orderEntry type="module" module-name="json-simple-1.1.1" exported="" />
101 102 </component>
102 103 </module>
app/build.gradle View file @ 153d373
... ... @@ -25,5 +25,6 @@
25 25 compile 'com.android.support:appcompat-v7:23.1.0'
26 26 compile 'com.android.support:design:23.1.0'
27 27 compile project(':jsoup-1.8.3')
  28 + compile project(':json-simple-1.1.1')
28 29 }
app/src/main/AndroidManifest.xml View file @ 153d373
... ... @@ -6,6 +6,10 @@
6 6 android:minSdkVersion="8"
7 7 android:targetSdkVersion="17" />
8 8 <uses-permission android:name="android.permission.INTERNET"/>
  9 + <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  10 + <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  11 +
  12 +
9 13  
10 14 <application
11 15 android:allowBackup="true"
app/src/main/java/com/example/junsang/simsimi/MainActivity.java View file @ 153d373
1 1 package com.example.junsang.simsimi;
2 2  
  3 +import android.annotation.TargetApi;
3 4 import android.os.AsyncTask;
  5 +import android.os.Build;
4 6 import android.support.v7.app.ActionBarActivity;
5 7 import android.os.Bundle;
  8 +import android.util.Log;
6 9 import android.view.Menu;
7 10 import android.view.MenuItem;
8 11 import android.widget.TextView;
9 12  
  13 +
10 14 import org.json.JSONArray;
11 15 import org.json.JSONException;
12   -//import org.w3c.dom.Document;
13 16  
14   -import org.jsoup.Jsoup;
15   -import org.jsoup.nodes.Document;
16   -import org.jsoup.nodes.Element;
17   -import org.jsoup.select.Elements;
  17 +import org.json.JSONObject;
18 18  
19 19 import java.io.BufferedReader;
20 20 import java.io.IOException;
21 21  
22 22  
23 23  
24 24  
... ... @@ -22,23 +22,28 @@
22 22 import java.io.InputStreamReader;
23 23 import java.net.HttpURLConnection;
24 24 import java.net.URL;
25   -import java.util.Iterator;
26 25  
27 26  
28 27 public class MainActivity extends ActionBarActivity {
29 28  
30   - private static final String URL = "http://aqicn.org/city/seoul";
31   - String results;
32   - TextView pm_tv;
  29 + private static final String LOG_TAG = "junsang";
33 30  
  31 + int flag = 0; //url flag
  32 +
34 33 //
35   - public String parse_pm(String str){
  34 + public String parse_pm(String str) {
36 35 String parse_1 = str.split("<td id=\'cur_pm25\' class=\'tdcur\' style=\'font-weight:bold;font-size:11px;\' align=center>")[1];
37 36 String parse_2 = parse_1.split("</td>")[0];
38 37  
39 38 return parse_2;
40 39 }
41 40  
  41 + private static final String URL = "http://jp3.aqicn.org/aqicn/services/geolocate/?autolocate&n=1";
  42 + String nearestPos = "";
  43 + String results;
  44 + TextView pm_tv;
  45 +
  46 + @TargetApi(Build.VERSION_CODES.M)
42 47 @Override
43 48 protected void onCreate(Bundle savedInstanceState) {
44 49 super.onCreate(savedInstanceState);
... ... @@ -66,7 +71,7 @@
66 71 return super.onOptionsItemSelected(item);
67 72 }
68 73  
69   - private class DownloadPageTask extends AsyncTask<String, Void, String> {
  74 + private class DownloadPageTask extends AsyncTask<String, Void, String> {
70 75 @Override
71 76 protected String doInBackground(String... urls) {
72 77 try {
73 78  
74 79  
75 80  
76 81  
77 82  
78 83  
... ... @@ -79,32 +84,41 @@
79 84 @Override
80 85 protected void onPostExecute(String result) {
81 86 results = result;
  87 + String nearestUrl = "";
82 88  
83   - // 지금 result : html
84   - // parsing -> JsonArray
85   - String jsonStr = "hi";
86   - try {
87   - JSONArray a = new JSONArray(jsonStr);
88   - } catch (JSONException e) {
89   - e.printStackTrace();
  89 + JSONArray jArr = null;
  90 +
  91 + if(flag==0) {
  92 +
  93 + try {
  94 + jArr = new JSONArray(result);
  95 +
  96 + for (int i = 0; i < jArr.length(); i++) {
  97 + JSONObject curl = jArr.getJSONObject(i);
  98 + nearestPos = curl.getString("name");
  99 + nearestUrl = curl.getString("curl");
  100 + }
  101 +
  102 + } catch (JSONException e) {
  103 + e.printStackTrace();
  104 + }
  105 + System.out.println(nearestPos);
  106 + System.out.println(nearestUrl);
  107 + flag++;
  108 + new DownloadPageTask().execute(nearestUrl);
90 109 }
  110 + else {
  111 + results = result;
91 112  
92   - jsonStr = parse_pm(result);
93   - /*
94   - Document doc = Jsoup.parse(result);
95   - Elements rows = doc.select("td.line-content ");
  113 + String pm25 ="";
96 114  
  115 + pm25 = parse_pm(result);
97 116  
98   - for (Element row : rows) {
99   - Iterator<Element> iterElem = row.getElementsByTag("div").iterator();
100   - StringBuilder builder = new StringBuilder();
101   - System.out.println(builder);
102   - builder.append(iterElem.next());
103   - jsonStr= jsonStr+builder.toString();
104   - }*/
105   - // String testStr = (?:"<td id='cur_pm25' class='tdcur' style='font-weight:bold;font-size:11px;' align=center>") result;
  117 + System.out.println(nearestPos);
  118 + pm_tv.setText("지금 " + nearestPos + "의 초미세먼지 농도는 " + pm25 + "㎍/㎥야~");
106 119  
107   - pm_tv.setText("지금 서울의 초미세먼지 농도는 "+jsonStr+"㎍/㎥야~");
  120 + flag =0;
  121 + }
108 122 }
109 123 }
110 124  
... ... @@ -133,7 +147,6 @@
133 147 sb.append(line);
134 148 }
135 149 str = sb.toString();
136   -
137 150  
138 151 } else {
139 152 conn.disconnect();
app/src/main/res/layout/activity_main.xml View file @ 153d373
... ... @@ -16,7 +16,7 @@
16 16 android:layout_height="match_parent">
17 17  
18 18 <ImageView
19   - android:layout_marginTop="200dp"
  19 + android:layout_marginTop="250dp"
20 20 android:layout_height="160dp"
21 21 android:layout_width="160dp"
22 22 android:background="@drawable/image1"/>
... ... @@ -29,9 +29,6 @@
29 29 android:layout_height="wrap_content"
30 30 android:background="@drawable/chat"
31 31 android:textSize="20sp" />
32   -
33   -
34   -
35 32  
36 33 </RelativeLayout>
37 34  
json-simple-1.1.1/build.gradle View file @ 153d373
  1 +configurations.create("default")
  2 +artifacts.add("default", file('json-simple-1.1.1.jar'))
json-simple-1.1.1/json-simple-1.1.1.iml View file @ 153d373
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<module external.linked.project.id=":json-simple-1.1.1" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="Simsimi" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
  3 + <component name="FacetManager">
  4 + <facet type="android-gradle" name="Android-Gradle">
  5 + <configuration>
  6 + <option name="GRADLE_PROJECT_PATH" value=":json-simple-1.1.1" />
  7 + </configuration>
  8 + </facet>
  9 + <facet type="java-gradle" name="Java-Gradle">
  10 + <configuration>
  11 + <option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
  12 + <option name="BUILDABLE" value="false" />
  13 + </configuration>
  14 + </facet>
  15 + </component>
  16 + <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
  17 + <exclude-output />
  18 + <content url="file://$MODULE_DIR$">
  19 + <excludeFolder url="file://$MODULE_DIR$/.gradle" />
  20 + <excludeFolder url="file://$MODULE_DIR$/build" />
  21 + </content>
  22 + <orderEntry type="inheritedJdk" />
  23 + <orderEntry type="sourceFolder" forTests="false" />
  24 + <orderEntry type="library" exported="" name="json-simple-1.1.1.json-simple-1.1.1" level="project" />
  25 + </component>
  26 +</module>
json-simple-1.1.1/json-simple-1.1.1.jar View file @ 153d373

No preview for this file type

settings.gradle View file @ 153d373
1   -include ':app', ':jsoup-1.8.3'
  1 +include ':app', ':jsoup-1.8.3', ':json-simple-1.1.1'