-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
272 lines (232 loc) · 6.32 KB
/
Copy pathMainActivity.java
File metadata and controls
272 lines (232 loc) · 6.32 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.example.project;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import java.net.InetAddress;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
boolean b;
public final static String STRING1="com.example.project.STRING1";
public final static String STRING2="com.example.project.STRING2";
TextView textView4;
AutoCompleteTextView autotext;
AutoCompleteTextView autotext2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
TextView textView1=(TextView)findViewById(R.id.textView1);
textView1.setTextColor(Color.RED);
TextView textView2=(TextView)findViewById(R.id.textView2);
textView2.setTextColor(Color.rgb(0, 191, 255));
TextView textView3=(TextView)findViewById(R.id.textView3);
textView3.setTextColor(Color.rgb(0, 191, 255));
final Intent intent=new Intent("com.example.project.NEXT");
textView4=(TextView)findViewById(R.id.textView4);
textView4.setTextColor(Color.RED);
textView4.setSingleLine();
final Button button=(Button)findViewById(R.id.button1);
button.setTextSize(15);
button.setTextColor(Color.WHITE);
button.setClickable(true);
autotext=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
autotext2=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView2);
autotext.setSingleLine();
autotext2.setSingleLine();
autotext.setTextSize(15);
autotext2.setTextSize(15);
GetData();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String st=autotext.getText().toString();
String st1=autotext2.getText().toString();
String s=st.trim();
String s1=st1.trim();
if(s.equals("") || s1.equals(""))
{
textView4.setText("Fill Each and Every Field");
}
else if(s.equals(s1))
{
textView4.setText("Source and Destination Same!!!");
}
else
{
GetData2(s,s1);
if(b)
{
intent.putExtra(STRING1, s);
intent.putExtra(STRING2, s1);
startActivity(intent);
}
}
}
});
}
public void GetData()
{
String result="";
InputStream isr=null;
try
{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://172.168.250.117/getData2.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
isr=entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag","Error in http connection"+e.toString());
}
try
{
BufferedReader reader= new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null)
{
sb.append(line+"\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag","Error converting result "+e.toString());
}
try
{
JSONArray jArray=new JSONArray(result);
final String[] srt=new String[jArray.length()];
for(int i=0;i<jArray.length();i++)
{
JSONObject json=jArray.getJSONObject(i);
srt[i]=json.getString("city");
}
final List<String> list=new ArrayList<String>();
for(int i=0;i<srt.length;i++)
{
list.add(srt[i]);
}
Collections.sort(list);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,srt);
autotext.setThreshold(1);
autotext.setAdapter(adapter);
autotext2.setThreshold(1);
autotext2.setAdapter(adapter);
}
catch(Exception e)
{
Log.e("log_tag","error parsing data "+e.toString());
}
}
public void GetData2(String s,String s1)
{
String result="";
InputStream isr=null;
try
{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://172.168.250.117/getData2.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
isr=entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag","Error in http connection"+e.toString());
}
try
{
BufferedReader reader= new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null)
{
sb.append(line+"\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag","Error converting result "+e.toString());
}
try
{
JSONArray jArray=new JSONArray(result);
final String[] srt=new String[jArray.length()];
for(int i=0;i<jArray.length();i++)
{
JSONObject json=jArray.getJSONObject(i);
srt[i]=json.getString("city");
}
int c1=0;
int c2=0;
for(int i=0;i<srt.length;i++)
{
if(s.equals(srt[i]))
c1++;
if(s1.equals(srt[i]))
c2++;
}
if(c1==0 && c2==0)
{
b=false;
textView4.setText("Source and Destination Points Invalid");
}
else if(c2==0)
{
b=false;
textView4.setText("Destination Point Invalid");
}
else if(c1==0)
{
b=false;
textView4.setText("Source Point Invalid");
}
else
{
b=true;
}
}
catch(Exception e)
{
Log.e("log_tag","error parsing data "+e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}