Advertisement
Guest User

Untitled

a guest
Aug 12th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 KB | None | 0 0
  1. package com.example.daumantas.zaidimas;
  2. import android.app.AlertDialog;
  3. import android.content.Context;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.webkit.WebSettings;
  11. import android.webkit.WebView;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.ListView;
  14. import android.widget.ProgressBar;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import org.json.JSONArray;
  19. import android.content.Intent;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22.  
  23. import java.io.BufferedReader;
  24. import java.io.BufferedWriter;
  25. import java.io.IOError;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.InputStreamReader;
  29. import java.io.OutputStream;
  30. import java.io.OutputStreamWriter;
  31. import java.net.HttpURLConnection;
  32. import java.net.MalformedURLException;
  33. import java.net.URL;
  34. import java.net.URLEncoder;
  35.  
  36. import static android.content.ContentValues.TAG;
  37.  
  38.  
  39. public class BackgroundWorker extends AsyncTask<String,Void,String> {
  40.  
  41. public String Grain;
  42. public String Timber;
  43. public String Stone;
  44. public String Iron;
  45. public String Gold;
  46.  
  47. public String a = "111";
  48.  
  49. TextView t1;
  50. TextView t2;
  51. TextView t3;
  52. TextView t4;
  53. TextView t5;
  54. Context context;
  55. AlertDialog alertDialog;
  56. BackgroundWorker (Context ctx){
  57. context = ctx;
  58.  
  59. }
  60.  
  61. @Override
  62. protected String doInBackground(String... params) {
  63. String type = params[0];
  64. String login_url = "http://192.168.1.65/getdataa.php";
  65. String register_url = "http://192.168.1.65/register.php";
  66. if (type.equals("login")) { //Jei tipas == login, bando prisijungt, else registruotis
  67. try {
  68. URL url = new URL(login_url);
  69. try {
  70. String user_name = params[1];
  71. String password = params[2];
  72. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  73. httpURLConnection.setRequestMethod("POST");
  74. httpURLConnection.setDoOutput(true);
  75. httpURLConnection.setDoInput(true);
  76. OutputStream outputStream = httpURLConnection.getOutputStream();
  77. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  78. String post_data = URLEncoder.encode("coordX", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8")+"&"
  79. +URLEncoder.encode("coordY", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8");
  80. bufferedWriter.write(post_data);
  81. bufferedWriter.flush();
  82. bufferedWriter.close();
  83. outputStream.close();
  84. InputStream inputStream = httpURLConnection.getInputStream();
  85. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  86. String result="";
  87. String line="";
  88. while((line = bufferedReader.readLine())!=null) {
  89. result += line;
  90. }
  91. bufferedReader.close();
  92. inputStream.close();
  93. httpURLConnection.disconnect();
  94. return result;
  95. } catch (MalformedURLException e) {
  96. e.printStackTrace();
  97. }
  98.  
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. } else if (type.equals("register")) {
  103. try {
  104. URL url = new URL(register_url);
  105. try {
  106. String name = params[1];
  107. String username = params[2];
  108. String password = params[3];
  109. String age = params[4];
  110. String email = params[5];
  111. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  112. httpURLConnection.setRequestMethod("POST");
  113. httpURLConnection.setDoOutput(true);
  114. httpURLConnection.setDoInput(true);
  115. OutputStream outputStream = httpURLConnection.getOutputStream();
  116. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  117. String post_data = URLEncoder.encode("name", "UTF-8")+"="+URLEncoder.encode(name, "UTF-8")+"&"
  118. + URLEncoder.encode("age", "UTF-8")+"="+URLEncoder.encode(username, "UTF-8")+"&"
  119. + URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8")+"&"
  120. + URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(age, "UTF-8")+"&"
  121. +URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email, "UTF-8");
  122. bufferedWriter.write(post_data);
  123. bufferedWriter.flush();
  124. bufferedWriter.close();
  125. outputStream.close();
  126. InputStream inputStream = httpURLConnection.getInputStream();
  127. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  128. String result="";
  129. String line="";
  130. while((line = bufferedReader.readLine())!=null) {
  131. result += line;
  132. }
  133. bufferedReader.close();
  134. inputStream.close();
  135. httpURLConnection.disconnect();
  136. return result;
  137. } catch (MalformedURLException e) {
  138. e.printStackTrace();
  139. }
  140.  
  141. } catch (IOException e) {
  142. e.printStackTrace();
  143. }
  144.  
  145. } else if (type.equals("register")) {
  146. try {
  147. URL url = new URL(register_url);
  148. try {
  149. String name = params[1];
  150. String username = params[2];
  151. String password = params[3];
  152. String age = params[4];
  153. String email = params[5];
  154. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  155. httpURLConnection.setRequestMethod("POST");
  156. httpURLConnection.setDoOutput(true);
  157. httpURLConnection.setDoInput(true);
  158. OutputStream outputStream = httpURLConnection.getOutputStream();
  159. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  160. String post_data = URLEncoder.encode("name", "UTF-8")+"="+URLEncoder.encode(name, "UTF-8")+"&"
  161. + URLEncoder.encode("age", "UTF-8")+"="+URLEncoder.encode(username, "UTF-8")+"&"
  162. + URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8")+"&"
  163. + URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(age, "UTF-8")+"&"
  164. +URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email, "UTF-8");
  165. bufferedWriter.write(post_data);
  166. bufferedWriter.flush();
  167. bufferedWriter.close();
  168. outputStream.close();
  169. InputStream inputStream = httpURLConnection.getInputStream();
  170. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  171. String result="";
  172. String line="";
  173. while((line = bufferedReader.readLine())!=null) {
  174. result += line;
  175. }
  176. bufferedReader.close();
  177. inputStream.close();
  178. httpURLConnection.disconnect();
  179. return result;
  180. } catch (MalformedURLException e) {
  181. e.printStackTrace();
  182. }
  183.  
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. }
  187.  
  188. }
  189.  
  190.  
  191. return null;
  192. }
  193.  
  194. @Override
  195. protected void onPreExecute() {
  196. alertDialog = new AlertDialog.Builder(context).create();
  197. alertDialog.setTitle("Coordinate Status");
  198.  
  199. }
  200.  
  201. @Override
  202. protected void onPostExecute(String result) {
  203. alertDialog.setMessage(result);
  204. alertDialog.show();
  205. // [{"Grain":3,"Timber":5,"Stone":5,"Iron":5,"Gold":5}]
  206. MapActivity insert = new MapActivity();
  207. getStrings(result);
  208. enterMats();
  209. }
  210.  
  211.  
  212. @Override
  213. protected void onProgressUpdate(Void... values) {
  214. super.onProgressUpdate(values);
  215. }
  216.  
  217.  
  218.  
  219. public void getStrings(String result){
  220. result = result.replace("[","");
  221. result = result.replace("]","");
  222. JSONObject jObject = null;
  223. try {
  224. jObject = new JSONObject(result);
  225. Grain = jObject.getString("Grain");
  226. Timber = jObject.getString("Timber");
  227. Stone = jObject.getString("Stone");
  228. Iron = jObject.getString("Iron");
  229. Gold = jObject.getString("Gold");
  230.  
  231. Log.d(TAG, "______________GRAIN___________: " + Grain);
  232. Log.d(TAG, "______________TIMBER__________: " + Timber);
  233. Log.d(TAG, "______________STONE___________: " + Stone);
  234. Log.d(TAG, "______________IRON____________: " + Iron);
  235. Log.d(TAG, "______________GOLD____________: " + Gold);
  236.  
  237. } catch (JSONException e) {
  238. e.printStackTrace();
  239. }
  240. }
  241.  
  242. public void enterMats(){
  243. MapActivity sendname = new MapActivity();
  244. sendname.getMats(Gold,Timber,Stone,Iron,Gold);
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement