/
MainActivity.java
202 lines (165 loc) · 6.62 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.example.shamin.weatherforecast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.net.Uri;
import android.view.View;
import android.content.Intent;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.Button;
import android.widget.RadioGroup;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
public class MainActivity extends AppCompatActivity {
private static String urlString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
addListenerOnButton();
street = (EditText) findViewById(R.id.street);
city = (EditText) findViewById(R.id.city);
submit = (Button) findViewById(R.id.submit);
state = (Spinner) findViewById(R.id.state);
error = (TextView) findViewById(R.id.error);
degreeGroup = (RadioGroup) findViewById(R.id.degree);
us = (RadioButton) findViewById(R.id.us);
si = (RadioButton) findViewById(R.id.si);
//degree = (RadioGroup) findViewById(R.id.degree);
}
public void onButtonClick(View v) {
Intent i = new Intent(MainActivity.this, Display.class);
startActivity(i);
}
public void onButtonClick1(View v){
if (v.getId() == R.id.submit) {
if (!validate()) {
if(si.isChecked())
degree="si";
else
degree = "us";
Log.d("pppp", degree);
// Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_LONG).show();
// sendPostRequest(street, city, state, degree);
//submit.setOnClickListener(new View.OnClickListener() {
//public void onClick(View v) {
lstreet=street.getText().toString().trim().replaceAll(" ","%20");
lcity=city.getText().toString().trim().replaceAll(" ","%20");
lstate=state.getSelectedItem().toString().replaceAll(" ","%20");
urlString = "http://cs-server.usc.edu:13083/HW8/sendjson.php?street="+lstreet+"&city="+lcity+"&state="+lstate+"°ree="+degree;
Log.d("check",urlString);
new ProcessJSON().execute(urlString);
//}
//});
}
}
}
ImageButton imageButton;
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.logo);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(MainActivity.this, "http://forecast.io/", Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://forecast.io/"));
startActivity(intent);
}
});
}
EditText street, city;
Button submit;
TextView error;
Spinner state;
RadioGroup degreeGroup;
RadioButton us;
RadioButton si;
String degree;
String lstreet;
String lcity;
String lstate;
public boolean validate() {
int x = 1;
if (street.getText().toString().trim().length() <= 0) {
// Toast t1 = Toast.makeText(MainActivity.this, "Please enter a street address", Toast.LENGTH_LONG);
// t1.setGravity(Gravity.LEFT | Gravity.BOTTOM, 250, 350);
// t1.show();
error.setText("Please enter a street address");
//validate();
x = 0;
} else if (city.getText().toString().trim().length() <= 0) {
// Toast t2 = Toast.makeText(MainActivity.this, "Please enter a city", Toast.LENGTH_LONG);
// t2.setGravity(Gravity.LEFT | Gravity.BOTTOM, 250, 350);
// t2.show();
error.setText("Please enter a city");
//validate();
x = 0;
} else if (state.getSelectedItem().toString().equals("Select")) {
// Toast t3 = Toast.makeText(MainActivity.this, "Please select a state", Toast.LENGTH_LONG);
// t3.setGravity(Gravity.LEFT | Gravity.BOTTOM, 250, 350);
// t3.show();
error.setText("Please select a state");
//validate();
x = 0;
}
if(x==0)
{
return true;
}
else {
return false;
}
}
private class ProcessJSON extends AsyncTask<String, Void, String>{
protected String doInBackground(String... strings){
String stream = null;
String urlString = strings[0];
HTTPDataHandler hh = new HTTPDataHandler();
stream = hh.GetHTTPData(urlString);
return stream;
}
protected void onPostExecute(String stream){
// TextView tv = (TextView) findViewById(R.id.tv);
Log.d("Debug", "Success");
if(stream != null){
try{
Log.d("str",stream);
//JSONObject reader = new JSONObject(stream);
Intent new_Intent = new Intent(MainActivity.this, ResultActivity.class);
new_Intent.putExtra("result", stream);
// new_Intent.putExtra("street", street.getText().toString().trim());
new_Intent.putExtra("city", city.getText().toString().trim());
new_Intent.putExtra("state", state.getSelectedItem().toString());
new_Intent.putExtra("degree", degree);
startActivity(new_Intent);
}catch(Exception e){
e.printStackTrace();
}
}
//else if(stream.equalsIgnoreCase(null)||stream.equalsIgnoreCase(""))
// Toast.makeText(MainActivity.this, "Bogus Values Entered", Toast.LENGTH_LONG).show();
}
}
public void onClickClear(View v)
{
street.setText("");
city.setText("");
state.setSelection(0);
us.setChecked(true);
si.setChecked(false);
error.setText("");
}
}