Commit a7ab6b18f0491e770ddcdd99d7820c9a6c9be5d7

Authored by junsang
1 parent e12cc46b80

UI modified, read html string to results

Showing 5 changed files with 150 additions and 53 deletions

.idea/misc.xml View file @ a7ab6b1
... ... @@ -37,7 +37,7 @@
37 37 <ConfirmationsSetting value="0" id="Add" />
38 38 <ConfirmationsSetting value="0" id="Remove" />
39 39 </component>
40   - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
  40 + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
41 41 <output url="file://$PROJECT_DIR$/build/classes" />
42 42 </component>
43 43 <component name="ProjectType">
app/src/main/AndroidManifest.xml View file @ a7ab6b1
... ... @@ -2,6 +2,11 @@
2 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 3 package="com.example.junsang.simsimi" >
4 4  
  5 + <uses-sdk
  6 + android:minSdkVersion="8"
  7 + android:targetSdkVersion="17" />
  8 + <uses-permission android:name="android.permission.INTERNET"/>
  9 +
5 10 <application
6 11 android:allowBackup="true"
7 12 android:icon="@mipmap/ic_launcher"
app/src/main/java/com/example/junsang/simsimi/MainActivity.java View file @ a7ab6b1
1 1 package com.example.junsang.simsimi;
2 2  
  3 +import android.os.AsyncTask;
  4 +import android.support.v7.app.ActionBarActivity;
3 5 import android.os.Bundle;
4   -import android.support.design.widget.FloatingActionButton;
5   -import android.support.design.widget.Snackbar;
6   -import android.support.v7.app.AppCompatActivity;
7   -import android.support.v7.widget.Toolbar;
8   -import android.view.View;
9 6 import android.view.Menu;
10 7 import android.view.MenuItem;
  8 +import android.widget.TextView;
11 9  
12   -public class MainActivity extends AppCompatActivity {
  10 +import org.json.JSONArray;
  11 +import org.json.JSONException;
13 12  
  13 +import java.io.BufferedReader;
  14 +import java.io.IOException;
  15 +import java.io.InputStream;
  16 +import java.io.InputStreamReader;
  17 +import java.net.HttpURLConnection;
  18 +import java.net.URL;
  19 +
  20 +
  21 +public class MainActivity extends ActionBarActivity {
  22 +
  23 + private static final String URL = "http://aqicn.org/city/seoul";
  24 + String results;
  25 + TextView pm_tv;
  26 +
14 27 @Override
15 28 protected void onCreate(Bundle savedInstanceState) {
16 29 super.onCreate(savedInstanceState);
17 30 setContentView(R.layout.activity_main);
18   - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
19   - setSupportActionBar(toolbar);
20 31  
21   - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
22   - fab.setOnClickListener(new View.OnClickListener() {
23   - @Override
24   - public void onClick(View view) {
25   - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
26   - .setAction("Action", null).show();
27   - }
28   - });
  32 + pm_tv = (TextView) findViewById(R.id.pm_tv);
  33 +
  34 + new DownloadPageTask().execute(URL);
29 35 }
30 36  
31 37 @Override
32 38 public boolean onCreateOptionsMenu(Menu menu) {
33   - // Inflate the menu; this adds items to the action bar if it is present.
34 39 getMenuInflater().inflate(R.menu.menu_main, menu);
35 40 return true;
36 41 }
37 42  
38 43 @Override
39 44 public boolean onOptionsItemSelected(MenuItem item) {
40   - // Handle action bar item clicks here. The action bar will
41   - // automatically handle clicks on the Home/Up button, so long
42   - // as you specify a parent activity in AndroidManifest.xml.
43 45 int id = item.getItemId();
44 46  
45   - //noinspection SimplifiableIfStatement
46 47 if (id == R.id.action_settings) {
47 48 return true;
48 49 }
49 50  
50 51 return super.onOptionsItemSelected(item);
  52 + }
  53 +
  54 + private class DownloadPageTask extends AsyncTask<String, Void, String> {
  55 + @Override
  56 + protected String doInBackground(String... urls) {
  57 + try {
  58 + return getHtmlFromHttp(urls[0]);
  59 + } catch (IOException e) {
  60 + return "Unable to retrieve web page.";
  61 + }
  62 + }
  63 +
  64 + @Override
  65 + protected void onPostExecute(String result) {
  66 + results = result;
  67 +
  68 + // 지금 result : html
  69 + // parsing -> JsonArray
  70 + String jsonStr = "hi";
  71 + try {
  72 + JSONArray a = new JSONArray(jsonStr);
  73 + } catch (JSONException e) {
  74 + e.printStackTrace();
  75 + }
  76 +
  77 + pm_tv.setText("결과값");
  78 + }
  79 + }
  80 +
  81 + private String getHtmlFromHttp(String myUrl) throws IOException {
  82 + InputStream is = null;
  83 + String str = "";
  84 +
  85 + try {
  86 + URL url = new URL(myUrl);
  87 + HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  88 + conn.setReadTimeout(10000);
  89 + conn.setConnectTimeout(10000);
  90 + conn.setRequestMethod("GET");
  91 + conn.setDoInput(true);
  92 +
  93 + conn.connect();
  94 + int resCode = conn.getResponseCode();
  95 +
  96 + if (resCode == HttpURLConnection.HTTP_OK) {
  97 + StringBuffer sb = new StringBuffer();
  98 +
  99 + is = conn.getInputStream();
  100 + BufferedReader br = new BufferedReader(new InputStreamReader(is));
  101 + String line;
  102 + while ((line = br.readLine()) != null) {
  103 + sb.append(line);
  104 + }
  105 + str = sb.toString();
  106 +
  107 +
  108 + } else {
  109 + conn.disconnect();
  110 + }
  111 + } finally {
  112 + if (is != null) {
  113 + is.close();
  114 + }
  115 + }
  116 +
  117 + return str;
51 118 }
52 119 }
app/src/main/res/layout/activity_main.xml View file @ a7ab6b1
1 1 <?xml version="1.0" encoding="utf-8"?>
2   -<android.support.design.widget.CoordinatorLayout
3   - xmlns:android="http://schemas.android.com/apk/res/android"
4   - xmlns:app="http://schemas.android.com/apk/res-auto"
5   - xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
6   - android:layout_height="match_parent" android:fitsSystemWindows="true"
  2 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + xmlns:tools="http://schemas.android.com/tools"
  4 + xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
  5 + android:layout_height="match_parent"
7 6 tools:context=".MainActivity">
8 7  
9   - <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
10   - android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
  8 + <ScrollView
  9 + android:layout_width="match_parent"
  10 + android:layout_height="match_parent"
  11 + android:orientation="vertical">
11 12  
12   - <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
13   - android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
14   - android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
  13 + <LinearLayout
  14 + android:orientation="horizontal"
  15 + android:layout_width="match_parent"
  16 + android:layout_height="match_parent">
15 17  
16   - </android.support.design.widget.AppBarLayout>
  18 + <ImageView
  19 + android:layout_height="160dp"
  20 + android:layout_width="160dp"
  21 + android:background="@drawable/image1"/>
17 22  
18   - <include layout="@layout/content_main" />
  23 + <TextView
  24 + android:id="@+id/pm_tv"
  25 + android:layout_gravity="right"
  26 + android:gravity="right"
  27 + android:layout_width="match_parent"
  28 + android:layout_height="wrap_content"
  29 + android:background="@drawable/chat"
  30 + android:textSize="20sp" />
19 31  
20   - <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
21   - android:layout_width="wrap_content" android:layout_height="wrap_content"
22   - android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
23   - android:src="@android:drawable/ic_dialog_email" />
24 32  
25   -</android.support.design.widget.CoordinatorLayout>
  33 +
  34 +
  35 + </LinearLayout>
  36 +
  37 + </ScrollView>
  38 +
  39 +
  40 +</LinearLayout>
app/src/main/res/layout/content_main.xml View file @ a7ab6b1
... ... @@ -9,18 +9,28 @@
9 9 app:layout_behavior="@string/appbar_scrolling_view_behavior"
10 10 tools:showIn="@layout/activity_main" tools:context=".MainActivity">
11 11  
12   - <ImageView
13   - android:layout_height="160dp"
14   - android:layout_width="160dp"
15   - android:background="@drawable/image1"/>
  12 + <ScrollView
  13 + android:id="@+id/scrollView1"
  14 + android:layout_width="match_parent"
  15 + android:layout_height="match_parent"
  16 + android:orientation="vertical">
16 17  
17   - <TextView
18   - android:layout_alignParentRight="true"
19   - android:layout_width="wrap_content"
20   - android:layout_height="wrap_content"
21   - android:text="hellowl"
22   - android:id="@+id/ChatMessage"
23   - android:background="@drawable/chat"
24   - android:textSize="20sp" />
  18 + <ImageView
  19 + android:layout_height="160dp"
  20 + android:layout_width="160dp"
  21 + android:background="@drawable/image1"/>
  22 +
  23 + <TextView
  24 + android:id="@+id/webpage_src"
  25 + android:layout_gravity="right"
  26 + android:gravity="right"
  27 + android:layout_width="match_parent"
  28 + android:layout_height="wrap_content"
  29 + android:background="@drawable/chat"
  30 + android:textSize="20sp" />
  31 +
  32 + </ScrollView>
  33 +
  34 +
25 35 </LinearLayout>