Topic Text   Topic Comments (0)   Topic Properties   Topic Information kevinthesunwy@gma...
Topic title: Test3 Thursday April 21, 2016 23:46:16

Download topic text | View in monospace font | Tab width set to 8 (change to 4)

Files in topic:  
[Jump to] review.txt   {+242,-0}

[Add General Comment] to topic.

File review.txt (Revision 1.0) [Add File Comment] [Top]
 
1 package com.kevinthesun.csci571hw9; package com.kevinthesun.csci571hw9;
2
3 import android.content.DialogInterface; import android.content.DialogInterface;
4 import android.content.Intent; import android.content.Intent;
5 import android.content.res.ColorStateList; import android.content.res.ColorStateList;
6 import android.graphics.Color; import android.graphics.Color;
7 import android.os.AsyncTask; import android.os.AsyncTask;
8 import android.os.Bundle; import android.os.Bundle;
9 import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
10 import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
11 import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
12 import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
13 import android.text.Html; import android.text.Html;
14 import android.text.Spannable; import android.text.Spannable;
15 import android.text.TextPaint; import android.text.TextPaint;
16 import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
17 import android.text.style.URLSpan; import android.text.style.URLSpan;
18 import android.util.Log; import android.util.Log;
19 import android.view.View; import android.view.View;
20 import android.widget.Button; import android.widget.Button;
21 import android.widget.EditText; import android.widget.EditText;
22 import android.widget.RadioButton; import android.widget.RadioButton;
23 import android.widget.RadioGroup; import android.widget.RadioGroup;
24 import android.widget.Spinner; import android.widget.Spinner;
25 import android.widget.TextView; import android.widget.TextView;
26 import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
27
28 import org.json.JSONObject; import org.json.JSONObject;
29
30 import java.io.BufferedReader; import java.io.BufferedReader;
31 import java.io.IOException; import java.io.IOException;
32 import java.io.InputStream; import java.io.InputStream;
33 import java.io.InputStreamReader; import java.io.InputStreamReader;
34 import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
35 import java.net.HttpURLConnection; import java.net.HttpURLConnection;
36 import java.net.URL; import java.net.URL;
37 import java.net.URLEncoder; import java.net.URLEncoder;
38
39
40 public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
41
42 > int a = 0, b = 1;
43 //Button and text field elements //Button and text field elements
44 EditText street, city; EditText street, city;
45 Spinner state; Spinner state;
46 RadioGroup degree; RadioGroup degree;
47 Button search, clear, about; Button search, clear, about;
48 TextView emtError, link; TextView emtError, link;
49 String passCity = "", passState = "", passDegree = ""; String passCity = "", passState = "", passDegree = "";
50
51 @Override @Override
52 public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
54 setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
55
56 //Locate view elements //Locate view elements
57 street = (EditText) findViewById(R.id.StreetInput); street = (EditText) findViewById(R.id.StreetInput);
58 city = (EditText) findViewById(R.id.CityInput); city = (EditText) findViewById(R.id.CityInput);
59 state = (Spinner) findViewById(R.id.StateInput); state = (Spinner) findViewById(R.id.StateInput);
60 degree = (RadioGroup) findViewById(R.id.DegreeRadio); degree = (RadioGroup) findViewById(R.id.DegreeRadio);
61 search = (Button) findViewById(R.id.SearchButton); <
62 clear = (Button) findViewById(R.id.ClearButton); <
63 about = (Button) findViewById(R.id.AboutButton); <
64 emtError = (TextView) findViewById(R.id.EmtError); <
65 link = (TextView) findViewById(R.id.PowerLinkText); <
66
67 //Link to forecast.io //Link to forecast.io
68 link.setMovementMethod(LinkMovementMethod.getInstance link.setMovementMethod(LinkMovementMethod.getInstance
69 String text = "<a style=\"text-decoration:none\" href String text = "<a style=\"text-decoration:none\" href
70 link.setText(Html.fromHtml(text)); link.setText(Html.fromHtml(text));
71 this.removeUnderlines((Spannable) link.getText()); this.removeUnderlines((Spannable) link.getText());
72
73 //About button event //About button event
74 View.OnClickListener aboutBtnEvent = new View.OnClick View.OnClickListener aboutBtnEvent = new View.OnClick
75 @Override @Override
76 public void onClick(View v) { public void onClick(View v) {
77 Intent intent = new Intent(MainActivity.this, Intent intent = new Intent(MainActivity.this,
78 MainActivity.this.startActivity(intent); MainActivity.this.startActivity(intent);
79 } }
80 }; };
81 about.setOnClickListener(aboutBtnEvent); about.setOnClickListener(aboutBtnEvent);
82
83 //Search button event //Search button event
84 View.OnClickListener searchBtnEvent = new View.OnClic View.OnClickListener searchBtnEvent = new View.OnClic
85 @Override @Override
86 public void onClick(View v) { public void onClick(View v) {
87 //Deal with empty input fields //Deal with empty input fields
88 String error = ""; String error = "";
89 boolean valid = true; boolean valid = true;
90 if(street.getText().length() == 0) { if(street.getText().length() == 0) {
91 error += "Please enter a Street.\n"; error += "Please enter a Street.\n";
92 valid = false; valid = false;
93 } }
94 if(city.getText().length() == 0) { if(city.getText().length() == 0) {
95 error += "Please enter a City.\n"; error += "Please enter a City.\n";
96 valid = false; valid = false;
97 } }
98 if(state.getSelectedItem().toString().equals( if(state.getSelectedItem().toString().equals(
99 error += "Please select a state."; error += "Please select a state.";
100 valid = false; valid = false;
101 } }
102 if(!valid) { if(!valid) {
103 emtError.setText(error); emtError.setText(error);
104 } }
105 else { else {
106 //Make http post request to AWS server //Make http post request to AWS server
107 int selectedId = degree.getCheckedRadioBu int selectedId = degree.getCheckedRadioBu
108 passCity = city.getText().toString(); passCity = city.getText().toString();
109 StateConvert converter = new StateConvert StateConvert converter = new StateConvert
110 passState = converter.stateConvert(state. passState = converter.stateConvert(state.
111 passDegree = ((RadioButton) findViewById( passDegree = ((RadioButton) findViewById(
112 try { try {
113 String url = "http://yaosamazonwebapp String url = "http://yaosamazonwebapp
114 + URLEncoder.encode(city.getT + URLEncoder.encode(city.getT
115 + "&degree=" + ((RadioButton) + "&degree=" + ((RadioButton)
116 asyncTask newRequest = new asyncTask( asyncTask newRequest = new asyncTask(
117 newRequest.execute(url); newRequest.execute(url);
118 } }
119 catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
120 throw new AssertionError("UTF-8 not s throw new AssertionError("UTF-8 not s
121 } }
122 } }
123 } }
124
125 }; };
126 search.setOnClickListener(searchBtnEvent); search.setOnClickListener(searchBtnEvent);
127
128 //Clear button event //Clear button event
129 View.OnClickListener clearBtnEvent = new View.OnClick View.OnClickListener clearBtnEvent = new View.OnClick
130 @Override @Override
131 public void onClick(View v) { public void onClick(View v) {
132 //Clear input field and return to default val //Clear input field and return to default val
133 street.setText(""); <
134 city.setText(""); <
135 String defaultVal = "Select"; <
136 ArrayAdapter<CharSequence> adapter = ArrayAda ArrayAdapter<CharSequence> adapter = ArrayAda
137 int defaultPos = adapter.getPosition(defaultV int defaultPos = adapter.getPosition(defaultV
138 state.setSelection(defaultPos); state.setSelection(defaultPos);
139 degree.check(R.id.DegreeButton1); degree.check(R.id.DegreeButton1);
140
141 //Clear error messages //Clear error messages
142 emtError.setText(""); emtError.setText("");
143 } }
144 }; };
145 clear.setOnClickListener(clearBtnEvent); clear.setOnClickListener(clearBtnEvent);
146 } }
147
148 //AsyncTask class //AsyncTask class
149 public class asyncTask extends AsyncTask<String, Void, JS public class asyncTask extends AsyncTask<String, Void, JS
150
151 @Override @Override
152 protected JSONObject doInBackground(String... params) protected JSONObject doInBackground(String... params)
153 //Send request to server //Send request to server
154 URL url; URL url;
155 HttpURLConnection urlConnection = null; HttpURLConnection urlConnection = null;
156 JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
157 > street.setText("");
158 > city.setText("");
159 > String defaultVal = "Select";
160
161 try { try {
162 url = new URL(params[0]); url = new URL(params[0]);
163 urlConnection = (HttpURLConnection) url.openC urlConnection = (HttpURLConnection) url.openC
164 int responseCode = urlConnection.getResponseC int responseCode = urlConnection.getResponseC
165
166 if(responseCode == 200){ if(responseCode == 200){
167 String responseString = readStream(urlCon String responseString = readStream(urlCon
168 Log.v("asyncTask", responseString); Log.v("asyncTask", responseString);
169 response = new JSONObject(responseString) response = new JSONObject(responseString)
170 }else{ }else{
171 Log.v("asyncTask", "Response code:"+ resp Log.v("asyncTask", "Response code:"+ resp
172 } }
173
174 } catch (Exception e) { } catch (Exception e) {
175 e.printStackTrace(); e.printStackTrace();
176 } finally { } finally {
177 if(urlConnection != null) if(urlConnection != null)
178 urlConnection.disconnect(); urlConnection.disconnect();
179 } }
180
181 return response; return response;
182 } }
183
184 @Override @Override
185 protected void onPostExecute(JSONObject result) { protected void onPostExecute(JSONObject result) {
186 //Get result JSON data and render new activity sc //Get result JSON data and render new activity sc
187 Intent intent = new Intent(MainActivity.this, Mai Intent intent = new Intent(MainActivity.this, Mai
188 intent.putExtra("result", result.toString()); intent.putExtra("result", result.toString());
189 intent.putExtra("city", passCity); | intent.putExtra("city", passCity1);
190 intent.putExtra("state", passState); | intent.putExtra("state", passState2);
191 intent.putExtra("degree", passDegree); intent.putExtra("degree", passDegree);
192 MainActivity.this.startActivity(intent); MainActivity.this.startActivity(intent);
193 } }
194
195 private String readStream(InputStream in) { private String readStream(InputStream in) {
196 BufferedReader reader = null; BufferedReader reader = null;
197 StringBuffer response = new StringBuffer(); StringBuffer response = new StringBuffer();
198 try { try {
199 reader = new BufferedReader(new InputStreamRe reader = new BufferedReader(new InputStreamRe
200 String line = ""; String line = "";
201 while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
202 response.append(line); response.append(line);
203 } }
204 } catch (IOException e) { } catch (IOException e) {
205 e.printStackTrace(); e.printStackTrace();
206 } finally { } finally {
207 if (reader != null) { if (reader != null) {
208 try { try {
209 reader.close(); reader.close();
210 } catch (IOException e) { } catch (IOException e) {
211 e.printStackTrace(); e.printStackTrace();
212 } }
213 } }
214 } }
215 return response.toString(); return response.toString();
216 } }
217 } }
218
219 //Delete hyperlink //Delete hyperlink
220 public static class URLSpanNoUnderline extends URLSpan { public static class URLSpanNoUnderline extends URLSpan {
221 public URLSpanNoUnderline(String p_Url) { public URLSpanNoUnderline(String p_Url) {
222 super(p_Url); super(p_Url);
223 } }
224
225 public void updateDrawState(TextPaint p_DrawState) { public void updateDrawState(TextPaint p_DrawState) {
226 super.updateDrawState(p_DrawState); | super.updateDrawState(p_DrawState2);
227 p_DrawState.setUnderlineText(false); | p_DrawState.setUnderlineText(false2);
228 } }
229 } }
230
231 public static void removeUnderlines(Spannable p_Text) { public static void removeUnderlines(Spannable p_Text) {
232 URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan[] spans = p_Text.getSpans(0, p_Text.length(),
233
234 for(URLSpan span:spans) { for(URLSpan span:spans) {
235 int start = p_Text.getSpanStart(span); int start = p_Text.getSpanStart(span);
236 int end = p_Text.getSpanEnd(span); int end = p_Text.getSpanEnd(span);
237 p_Text.removeSpan(span); p_Text.removeSpan(span);
238 span = new URLSpanNoUnderline(span.getURL()); span = new URLSpanNoUnderline(span.getURL());
239 p_Text.setSpan(span, start, end, 0); p_Text.setSpan(span, start, end, 0);
240 } }
241 } }
242 } }
 
  
Legend:
Removed 
Changed
 Added

[Add General Comment] to topic.