Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.56 KB | None | 0 0
  1. package com.artrelm.artrelm2;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.widget.Toast;
  8. import java.io.BufferedReader;
  9. import java.io.BufferedWriter;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStream;
  14. import java.io.OutputStreamWriter;
  15. import java.net.HttpURLConnection;
  16. import java.net.MalformedURLException;
  17. import java.net.URL;
  18. import java.net.URLEncoder;
  19.  
  20.  
  21. public class BackgroundTask extends AsyncTask<String,Void,String> {
  22. AlertDialog alertDialog;
  23. Context ctx;
  24. BackgroundTask(Context ctx)
  25. {
  26. this.ctx =ctx;
  27. }
  28. @Override
  29. protected void onPreExecute() {
  30. alertDialog = new AlertDialog.Builder(ctx).create();
  31. alertDialog.setTitle("Login Information....");
  32. }
  33. @Override
  34. protected String doInBackground(String... params) {
  35. String reg_url = "http://comicsubs.com/artrelm/register.php";
  36. String login_url = "http://comicsubs.com/artrelm/login.php";
  37. String method = params[0];
  38. if (method.equals("register")) {
  39. String username = params[1];
  40. String password = params[2];
  41.  
  42. try {
  43. URL url = new URL(reg_url);
  44. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  45. httpURLConnection.setRequestMethod("POST");
  46. httpURLConnection.setDoOutput(true);
  47. //httpURLConnection.setDoInput(true);
  48. OutputStream OS = httpURLConnection.getOutputStream();
  49. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
  50. String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
  51. URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") ;
  52. bufferedWriter.write(data);
  53. bufferedWriter.flush();
  54. bufferedWriter.close();
  55. OS.close();
  56. InputStream IS = httpURLConnection.getInputStream();
  57. IS.close();
  58. //httpURLConnection.connect();
  59. httpURLConnection.disconnect();
  60. return "Registration Success...";
  61. } catch (MalformedURLException e) {
  62. e.printStackTrace();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. else if(method.equals("login"))
  68. {
  69. String username = params[1];
  70. String password = params[2];
  71. try {
  72. URL url = new URL(login_url);
  73. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  74. httpURLConnection.setRequestMethod("POST");
  75. httpURLConnection.setDoOutput(true);
  76. httpURLConnection.setDoInput(true);
  77. OutputStream outputStream = httpURLConnection.getOutputStream();
  78. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  79. String data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
  80. URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
  81. bufferedWriter.write(data);
  82. bufferedWriter.flush();
  83. bufferedWriter.close();
  84. outputStream.close();
  85. InputStream inputStream = httpURLConnection.getInputStream();
  86. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  87. String response = "";
  88. String line = "";
  89. while ((line = bufferedReader.readLine())!=null)
  90. {
  91. response+= line;
  92. }
  93. bufferedReader.close();
  94. inputStream.close();
  95. httpURLConnection.disconnect();
  96. return response;
  97. } catch (MalformedURLException e) {
  98. e.printStackTrace();
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. return null;
  104. }
  105. @Override
  106. protected void onProgressUpdate(Void... values) {
  107. super.onProgressUpdate(values);
  108. }
  109. @Override
  110. protected void onPostExecute(String result) {
  111. //alertDialog.setMessage(result);
  112. //alertDialog.show();
  113. //Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  114. if(result.equals("Registration Success..."))
  115. {
  116. Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  117. }
  118. else {
  119. if (result.equals("Signedin")) {
  120. Toast.makeText(ctx, "Signed In Successfully", Toast.LENGTH_LONG).show();
  121. Intent i = new Intent(BackgroundTask.this, MainActivity.class);
  122. }else Toast.makeText(ctx, "Wrong Username Or Password ", Toast.LENGTH_LONG).show();
  123. }
  124. }
  125.  
  126.  
  127. }
  128.  
  129. /*
  130. import android.app.AlertDialog;
  131. import android.content.Context;
  132. import android.os.AsyncTask;
  133. import android.widget.Toast;
  134.  
  135. import java.io.BufferedReader;
  136. import java.io.BufferedWriter;
  137. import java.io.IOException;
  138. import java.io.InputStream;
  139. import java.io.InputStreamReader;
  140. import java.io.OutputStream;
  141. import java.io.OutputStreamWriter;
  142. import java.net.HttpURLConnection;
  143. import java.net.MalformedURLException;
  144. import java.net.URL;
  145. import java.net.URLEncoder;
  146.  
  147. /**
  148. * Created by Ciobotaru on 27/03/2016.
  149. */ /*
  150. public class Background extends AsyncTask<String,Void,String> {
  151. AlertDialog alertDialog;
  152. Context ctx;
  153.  
  154.  
  155. Background(Context ctx) {
  156. this.ctx = ctx;
  157.  
  158. }
  159.  
  160. protected void onPreExecute() {
  161. alertDialog = new AlertDialog.Builder(ctx).create();
  162. alertDialog.setTitle("Login information ..");
  163. }
  164.  
  165.  
  166.  
  167. protected String doInBackground(String... params) {
  168. String reg_url = "http://comicsubs.com/artrelm/register.php";
  169. String log_url = "http://comicsubs.com/artrelm/login.php";
  170.  
  171. String method = params[0];
  172. if (method.equals("register")) {
  173. String username = params[1];
  174. String password = params[2];
  175. try {
  176. URL url = new URL(reg_url);
  177. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  178. httpURLConnection.setRequestMethod("POST");
  179. httpURLConnection.setDoOutput(true);
  180. OutputStream OS = httpURLConnection.getOutputStream();
  181. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
  182.  
  183. String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
  184. URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  185. bufferedWriter.write(data);
  186. bufferedWriter.flush();
  187. bufferedWriter.close();
  188. OS.close();
  189. InputStream IS = httpURLConnection.getInputStream();
  190. IS.close();
  191. //reg = true;
  192.  
  193. return "Registration Success";
  194.  
  195. } catch (MalformedURLException e) {
  196. e.printStackTrace();
  197. } catch (IOException e) {
  198. e.printStackTrace();
  199. }
  200. } else if (method.equals("sign")){
  201. String username = params[1];
  202. String password = params[2];
  203. try {
  204. URL url = new URL(log_url);
  205. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  206. httpURLConnection.setRequestMethod("POST");
  207. httpURLConnection.setDoOutput(true);
  208. httpURLConnection.setDoInput(true);
  209. OutputStream outputStream = httpURLConnection.getOutputStream();
  210. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  211. String data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
  212. URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
  213. bufferedWriter.write(data);
  214. bufferedWriter.flush();
  215. bufferedWriter.close();
  216. outputStream.close();
  217. InputStream inputStream = httpURLConnection.getInputStream();
  218. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  219. String response = "";
  220. String line = "";
  221. while ((line = bufferedReader.readLine())!=null)
  222. {
  223. response+= line;
  224. }
  225. bufferedReader.close();
  226. inputStream.close();
  227. httpURLConnection.disconnect();
  228. return response;
  229. } catch (MalformedURLException e) {
  230. e.printStackTrace();
  231. } catch (IOException e) {
  232. e.printStackTrace();
  233. }
  234. }
  235. return null;
  236. }
  237.  
  238. /*
  239. String username = params[1];
  240. String password = params[2];
  241. try {
  242. URL url = new URL(log_url);
  243. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  244. httpURLConnection.setRequestMethod("POST");
  245. httpURLConnection.setDoOutput(true);
  246. httpURLConnection.setDoInput(true);
  247. OutputStream outputStream = httpURLConnection.getOutputStream();
  248. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  249.  
  250. String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
  251. URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  252. bufferedWriter.write(data);
  253. bufferedWriter.flush();
  254. bufferedWriter.close();
  255. outputStream.close();
  256.  
  257. InputStream inputStream = httpURLConnection.getInputStream();
  258. inputStream.close();
  259. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  260. String response = "";
  261. String line = "";
  262. while ((line = bufferedReader.readLine())!=null) {
  263. response += line;
  264. }
  265. bufferedReader.close();
  266. inputStream.close();
  267. httpURLConnection.disconnect();
  268. return response;
  269.  
  270.  
  271. } catch (MalformedURLException e) {
  272. e.printStackTrace();
  273. } catch (IOException e) {
  274. e.printStackTrace();
  275. }
  276. }
  277. return null;
  278.  
  279.  
  280. protected void onProgressUpdate(Void... values) {
  281. super.onProgressUpdate(values);
  282. }
  283.  
  284. protected void onPostExecute(String result) {
  285. if ("Registration Success".equals(result)) {
  286. Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  287.  
  288. } else {
  289. alertDialog.setMessage(result);
  290. alertDialog.show();
  291. }
  292. }
  293.  
  294.  
  295. }
  296. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement