Advertisement
Guest User

Untitled

a guest
May 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.46 KB | None | 0 0
  1. import android.content.Context;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.os.AsyncTask;
  5. import android.util.Log;
  6. import android.view.ViewGroup;
  7.  
  8. import org.json.JSONArray;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11.  
  12. import java.io.BufferedReader;
  13. import java.io.BufferedWriter;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.InputStreamReader;
  17. import java.io.OutputStream;
  18. import java.io.OutputStreamWriter;
  19. import java.net.HttpURLConnection;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.net.URLDecoder;
  23. import java.net.URLEncoder;
  24.  
  25. /**
  26. * Created by ARTEM on 11.04.2017.
  27. */
  28.  
  29. public class NetworkHelper extends AsyncTask<String,Void,String> {
  30. Context context;
  31.  
  32. String result = "";
  33. JSONArray jsonResult;
  34. static Bitmap imageResult;
  35. //AlertDialog alertDialog;
  36.  
  37. NetworkHelper(Context ctx) {
  38. context = ctx;
  39. }
  40.  
  41. @Override
  42. protected String doInBackground(String... params) {
  43. String type = params[0];
  44. String ip = "192.168.1.106";
  45. String checkList_url = "http://" + ip + "/android/checkList.php";
  46. String addOrder_url = "http://" + ip + "/android/addOrder.php";
  47. String getUserOrders_url = "http://" + ip + "/android/getUserOrders.php";
  48. String getAccount_url = "http://" + ip + "/android/getAccount.php";
  49. String setAccount_url = "http://" + ip + "/android/setAccount.php";
  50. String image_url = "http://" + ip + "/android/pictures/";
  51. if (type.equals("checkList")) {
  52. try {
  53. URL url = new URL(checkList_url);
  54. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  55. httpURLConnection.setRequestMethod("GET");
  56. httpURLConnection.setDoOutput(true);
  57. httpURLConnection.setDoInput(true);
  58. InputStream inputStream = httpURLConnection.getInputStream();
  59. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  60. String line;
  61. result = "";
  62. while ((line = bufferedReader.readLine()) != null) {
  63. result += line;
  64. }
  65. try {
  66. jsonResult = new JSONArray();
  67. jsonResult = new JSONArray(result);
  68. } catch (JSONException e) {
  69. e.printStackTrace();
  70. }
  71. delay(100);
  72. bufferedReader.close();
  73. inputStream.close();
  74. httpURLConnection.disconnect();
  75. return "";
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. if (type.equals("getImage")) {
  81. try {
  82. URL url = new URL(image_url+params[1]);
  83. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  84. InputStream inputStream = httpURLConnection.getInputStream();
  85. imageResult = BitmapFactory.decodeStream(inputStream);
  86. delay(200);
  87. inputStream.close();
  88. httpURLConnection.disconnect();
  89. return "";
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. if (type.equals("addOrder")) {
  95. try {
  96. String dataAboutUser = params[1];
  97. String products = params[2];
  98. URL url = new URL(addOrder_url);
  99. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  100. httpURLConnection.setRequestMethod("POST");
  101. httpURLConnection.setDoOutput(true);
  102. httpURLConnection.setDoInput(true);
  103. OutputStream outputStream = httpURLConnection.getOutputStream();
  104. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  105. String post_data = URLEncoder.encode("dataAboutUser", "UTF-8") + "=" + URLEncoder.encode(dataAboutUser, "UTF-8") + "&" +
  106. URLEncoder.encode("products", "UTF-8") + "=" + URLEncoder.encode(products, "UTF-8");
  107. Log.i("POST DATA", post_data);
  108. Log.i("DECODE", URLDecoder.decode(post_data, "UTF-8"));
  109. bufferedWriter.write(post_data);
  110. bufferedWriter.flush();
  111. bufferedWriter.close();
  112. outputStream.close();
  113. InputStream inputStream = httpURLConnection.getInputStream();
  114. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  115. String line;
  116. while ((line = bufferedReader.readLine()) != null) {
  117. result += line;
  118. }
  119. bufferedReader.close();
  120. inputStream.close();
  121. httpURLConnection.disconnect();
  122. delay(100);
  123. return result;
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. if (type.equals("getUserOrders")) {
  129. try {
  130. String androidId = params[1];
  131. URL url = new URL(getUserOrders_url);
  132. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  133. httpURLConnection.setRequestMethod("POST");
  134. httpURLConnection.setDoOutput(true);
  135. httpURLConnection.setDoInput(true);
  136. OutputStream outputStream = httpURLConnection.getOutputStream();
  137. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  138. String post_data = URLEncoder.encode("androidId", "UTF-8") + "=" + URLEncoder.encode(androidId, "UTF-8");
  139. bufferedWriter.write(post_data);
  140. bufferedWriter.flush();
  141. bufferedWriter.close();
  142. outputStream.close();
  143. InputStream inputStream = httpURLConnection.getInputStream();
  144. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  145. String line;
  146. result = "";
  147. while ((line = bufferedReader.readLine()) != null) {
  148. result += line;
  149. }
  150. try {
  151. jsonResult = new JSONArray();
  152. jsonResult = new JSONArray(result);
  153. } catch (JSONException e) {
  154. e.printStackTrace();
  155. }
  156. bufferedReader.close();
  157. inputStream.close();
  158. httpURLConnection.disconnect();
  159. delay(100);
  160. return "";
  161. } catch (IOException e) {
  162. e.printStackTrace();
  163. }
  164. }
  165. if (type.equals("getAccount")) {
  166. try {
  167. String login = params[1];
  168. String password = params[2];
  169. URL url = new URL(getAccount_url);
  170. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  171. httpURLConnection.setRequestMethod("POST");
  172. httpURLConnection.setDoOutput(true);
  173. httpURLConnection.setDoInput(true);
  174. OutputStream outputStream = httpURLConnection.getOutputStream();
  175. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  176. String post_data = URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode(login, "UTF-8") + "&" +
  177. URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  178. bufferedWriter.write(post_data);
  179. bufferedWriter.flush();
  180. bufferedWriter.close();
  181. outputStream.close();
  182. InputStream inputStream = httpURLConnection.getInputStream();
  183. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  184. String line;
  185. result = "";
  186. while ((line = bufferedReader.readLine()) != null) {
  187. result += line;
  188. }
  189. try {
  190. MainActivity.account = new JSONObject(result);
  191. } catch (JSONException e) {
  192. e.printStackTrace();
  193. }
  194. bufferedReader.close();
  195. inputStream.close();
  196. httpURLConnection.disconnect();
  197. delay(100);
  198. return "";
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. }
  202. }
  203. if (type.equals("setAccount")) {
  204. try {
  205. String login = params[1];
  206. String password = params[2];
  207. String dataAboutAccount = params[3];
  208. URL url = new URL(setAccount_url);
  209. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  210. httpURLConnection.setRequestMethod("POST");
  211. httpURLConnection.setDoOutput(true);
  212. httpURLConnection.setDoInput(true);
  213. OutputStream outputStream = httpURLConnection.getOutputStream();
  214. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  215. String post_data = URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode(login, "UTF-8") + "&" +
  216. URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
  217. URLEncoder.encode("dataAboutAccount", "UTF-8") + "=" + URLEncoder.encode(dataAboutAccount, "UTF-8");
  218. bufferedWriter.write(post_data);
  219. bufferedWriter.flush();
  220. bufferedWriter.close();
  221. outputStream.close();
  222. InputStream inputStream = httpURLConnection.getInputStream();
  223. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  224. String line;
  225. result = "";
  226. while ((line = bufferedReader.readLine()) != null) {
  227. result += line;
  228. }
  229. bufferedReader.close();
  230. inputStream.close();
  231. httpURLConnection.disconnect();
  232. delay(100);
  233. return "";
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. }
  237. }
  238. return null;
  239. }
  240.  
  241. @Override
  242. protected void onPreExecute() {
  243. /*alertDialog = new AlertDialog.Builder(context).create();
  244. alertDialog.setTitle("Wait");
  245. alertDialog.show();*/
  246. Log.d("s","s");
  247. }
  248.  
  249. @Override
  250. protected void onPostExecute(String s) {
  251. //alertDialog.dismiss();
  252. //alertDialog.show();
  253. }
  254.  
  255. @Override
  256. protected void onProgressUpdate(Void... values) {
  257. super.onProgressUpdate(values);
  258. }
  259.  
  260. public void delay(Integer value) {
  261. try {
  262. Thread.sleep(value);
  263. } catch (InterruptedException e) {
  264. e.printStackTrace();
  265. }
  266. }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement