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
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = MainActivity.class.getName();
private static final String HOST = "http://medplus.hut4.ru/feld.php";
private static final String ENCODING = "utf8";
private TextView label;
private MyTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label = (TextView) findViewById(R.id.label);
findViewById(R.id.buttonChange).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonChange: {
if (task == null || task.isCancelled()) {
task = new MyTask();
task.execute();
}
}
break;
}
}
public static class UserInfo {
private long id;
private String login;
private String password;
private String firstName;
private String secondName;
private String lastName;
private String phone;
public UserInfo() {
super();
}
public UserInfo(long id, String login, String password,
String firstName, String secondName, String lastName,
String phone) {
super();
this.id = id;
this.login = login;
this.password = password;
this.firstName = firstName;
this.secondName = secondName;
this.lastName = lastName;
this.phone = phone;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "UserInfo [id=" + id + ", login=" + login + ", password="
+ password + ", firstName=" + firstName + ", secondName="
+ secondName + ", lastName=" + lastName + ", phone="
+ phone + "]";
}
}
// -------------------------
public class MyTask extends AsyncTask<Void, UserInfo, Void> {
public static final String KEY_ID = "FID";
public static final String KEY_LOGIN = "login";
public static final String KEY_PASSWORD = "password";
public static final String KEY_FIRST_NAME = "Fname";
public static final String KEY_LAST_NAME = "Lname";
public static final String KEY_SECOND_NAME = "Sname";
public static final String KEY_PHONE = "Fphone";
private JSONArray jsonArray;
private String webData = null;
private StringBuilder sb = null;
private Throwable exception;
@Override
protected void onPreExecute() {
label.setText(null);
}
@Override
protected Void doInBackground(Void... params) {
HttpEntity entity = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(HOST);
HttpResponse response = httpclient.execute(httppost);
entity = response.getEntity();
} catch (Exception e) {
Log.e(TAG, "Error in http connection" + e.toString(), e);
exception = e;
return null;
}
BufferedReader reader = null;
InputStream is = null;
try {
is = entity.getContent();
reader = new BufferedReader(
new InputStreamReader(is, ENCODING), 8);
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
webData = sb.toString();
} catch (Exception e) {
Log.e(TAG, "Error converting result " + e.toString(), e);
exception = e;
return null;
} finally {
try {
reader.close();
is.close();
} catch (IOException e) {
Log.e(TAG, "Unexpected Error" + e.toString(), e);
}
}
try {
jsonArray = new JSONArray(webData);
JSONObject jsonData = null;
for (int i = 0; i < jsonArray.length(); i++) {
jsonData = jsonArray.getJSONObject(i);
final UserInfo userInfo = new UserInfo();
userInfo.setId(jsonData.getLong(KEY_ID));
userInfo.setLogin(jsonData.getString(KEY_LOGIN));
userInfo.setPassword(jsonData.getString(KEY_PASSWORD));
userInfo.setFirstName(jsonData.getString(KEY_FIRST_NAME));
userInfo.setSecondName(jsonData.getString(KEY_SECOND_NAME));
userInfo.setLastName(jsonData.getString(KEY_LAST_NAME));
userInfo.setPhone(jsonData.getString(KEY_PHONE));
publishProgress(userInfo);
}
} catch (JSONException e) {
Log.e(TAG, "Bad json " + e.toString(), e);
exception = e;
return null;
} catch (ParseException e) {
Log.e(TAG, "Error converting result " + e.toString(), e);
exception = e;
return null;
}
return null;
}
@Override
protected void onProgressUpdate(UserInfo... values) {
UserInfo userInfo = values[0];
String s = label.getText().toString() + "\n" + userInfo.toString();
label.setText(s);
}
@Override
protected void onPostExecute(Void result) {
if (exception != null) {
Toast.makeText(MainActivity.this,
"Error while getting data from web server",
Toast.LENGTH_SHORT).show();
}
}
}
} |