Topic Text   Topic Comments (2)   Topic Properties   Topic Information kevinthesunwy@gma...
Topic title: Kevin's Test Wednesday April 20, 2016 23:37:09

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

Files in topic:  
[Jump to] MainActivity.java   {+237,-0}

[Add General Comment] to topic.

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

[Add General Comment] to topic.