-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReedite.java
More file actions
359 lines (287 loc) · 14 KB
/
Reedite.java
File metadata and controls
359 lines (287 loc) · 14 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package com.zamer.zamer;
import java.util.ArrayList;
import java.util.List;
import com.zamer.parser.JSONParser;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Reedite extends Activity {
TextView textView;
EditText txtName;
EditText txtPrice;
EditText txtDesc;
EditText txtMan;
EditText txtMetka;
EditText txtFloor;
EditText txtTelz;
EditText txtImena;
EditText txtDen;
EditText txtTim;
EditText txtCompany;
EditText txtOderstates;
EditText txtPaystates;
EditText txtIP;
EditText txtUrl;
Button btnSave;
Button btnDelete;
String pid;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
// url для получения одного продукта
private static final String url_product_detials = "http://zamer.com.ua/api";
// url для обновления продукта
private static final String url_update_product = "http://zamer.com.ua/api";
// url для удаления продукта
private static final String url_delete_product = "http://zamer.com.ua/api";
// JSON параметры
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_MAN = "man";
private static final String TAG_METKA = "metka";
private static final String TAG_FLOOR = "floor";
private static final String TAG_TELZ = "telz";
private static final String TAG_NAMER = "imena";
private static final String TAG_DEN = "den";
private static final String TAG_TIM = "tim";
private static final String TAG_COMPANY = "company";
private static final String TAG_ODERSTATES = "oderstates";
private static final String TAG_PAYSTATES = "paystates";
private static final String TAG_IP = "ip";
private static final String TAG_URL = "url";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.re_edit);
btnSave = (Button) findViewById(R.id.btnSave);
btnDelete = (Button) findViewById(R.id.btnDelete);
// показываем форму про детальную информацию о продукте
Intent i = getIntent();
// получаем id продукта (pid) с формы
pid = i.getStringExtra(TAG_PID);
// Получение полной информации о продукте в фоновом потоке
new GetProductDetails().execute();
// обрабочик на кнопку сохранение продукта
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// запускаем выполнение задачи на обновление продукта
new SaveProductDetails().execute();
}
});
// обработчик на кнопку удаление продукта
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// удалем продукт в фоновом потоке
new DeleteProduct().execute();
}
});
}
/**
* Фоновая асинхронная задача для получения полной информации о продукте
**/
class GetProductDetails extends AsyncTask<String, String, String> {
/**
* Перед началом показать в фоновом потоке прогресс диалог
**/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Reedite.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Получение детальной информации о продукте в фоновом режиме
**/
protected String doInBackground(String[] params) {
// обновляем UI форму
runOnUiThread(new Runnable() {
public void run() {
// проверяем статус success тега
int success;
try {
// Список параметров
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
// получаем продукт по HTTP запросу
JSONObject json = jsonParser.makeHttpRequest(url_product_detials, "GET", params);
Log.d("Single Product Details", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Успешно получинна детальная информация о продукте
JSONArray productObj = json.getJSONArray(TAG_PRODUCT);
// получаем первый обьект с JSON Array
JSONObject product = productObj.getJSONObject(0);
// продукт с pid найден
// Edit Text
txtName = (EditText) findViewById(R.id.txtName);
txtImena = (EditText) findViewById(R.id.txtImena);
txtDesc = (EditText) findViewById(R.id.txtDesc);
txtTelz = (EditText) findViewById(R.id.txtTelz);
//txtDen = (EditText) findViewById(R.id.txtDen);
//txtTim = (EditText) findViewById(R.id.txtTim);
txtFloor = (EditText) findViewById(R.id.txtFloor);
txtMetka = (EditText) findViewById(R.id.txtMetka);
// покаываем данные о продукте в EditText
txtName.setText(product.getString(TAG_NAME));
txtFloor.setText(product.getString(TAG_FLOOR));
txtMetka.setText(product.getString(TAG_METKA));
txtDesc.setText(product.getString(TAG_DESCRIPTION));
txtTelz.setText(product.getString(TAG_TELZ));
txtImena.setText(product.getString(TAG_NAMER));
//txtDen.setText(product.getString(TAG_DEN));
// txtTim.setText(product.getString(TAG_TIM));
//txtMan.setText(product.getString(TAG_MAN) + " " + product.getString(TAG_COMPANY));
} else {
// продукт с pid не найден
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* После завершения фоновой задачи закрываем диалог прогресс
**/
protected void onPostExecute(String file_url) {
// закрываем диалог прогресс
pDialog.dismiss();
}
}
/**
* В фоновом режиме выполняем асинхроную задачу на сохранение продукта
**/
class SaveProductDetails extends AsyncTask<String, String, String> {
/**
* Перед началом показываем в фоновом потоке прогрксс диалог
**/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Reedite.this);
pDialog.setMessage("Сохранение заявки ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Сохраняем продукт
**/
protected String doInBackground(String[] args) {
// получаем обновленные данные с EditTexts
String name = txtName.getText().toString();
//String tim = txtTim.getText().toString();
String description = txtDesc.getText().toString();
String ime = txtImena.getText().toString();
String tel = txtTelz.getText().toString();
String floor = txtFloor.getText().toString();
//String den = txtDen.getText().toString();
String met = txtMetka.getText().toString();
// формируем параметры
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(TAG_PID, pid));
params.add(new BasicNameValuePair(TAG_NAME, name));
//params.add(new BasicNameValuePair(TAG_TIM, tim));
params.add(new BasicNameValuePair(TAG_DESCRIPTION, description));
params.add(new BasicNameValuePair(TAG_NAMER, ime));
params.add(new BasicNameValuePair(TAG_TELZ, tel));
params.add(new BasicNameValuePair(TAG_FLOOR, floor));
// params.add(new BasicNameValuePair("den", den));
params.add(new BasicNameValuePair(TAG_METKA, met));
Log.d("tag params", params.toString());
// отправляем измененные данные через http запрос
JSONObject json = jsonParser.makeHttpRequest(url_update_product, "POST", params);
// проверяем json success тег
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Запускаем Activity вывода всех продуктов
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
startActivity(i);
finish();
} else {
// продукт не обновлен
}
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
/**
* После окончания закрываем прогресс диалог
**/
protected void onPostExecute(String file_url) {
// закрываем прогресс диалог
pDialog.dismiss();
}
}
/**
* Фоновая асинхронная задача на удаление продукта
**/
class DeleteProduct extends AsyncTask<String, String, String> {
/**
* На начале показываем прогресс диалог
**/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Reedite.this);
pDialog.setMessage("Удаляем заявку...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Удаление продукта
**/
protected String doInBackground(String[] args) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
// получение продукта используя HTTP запрос
JSONObject json = jsonParser.makeHttpRequest(url_delete_product, "POST", params);
Log.d("Delete Product", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Продукт удачно удален
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
// отправляем результирующий код 100 для уведомления об удалении продукта
setResult(100, i);
startActivity(i);
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* После оконачния скрываем прогресс диалог
**/
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
}