Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. //define constructor
  2. public BackgroundTask (Context ctx){
  3. this.ctx = ctx;
  4. activity = (Activity)ctx;
  5. }
  6.  
  7. @Override
  8. protected void onPreExecute() {
  9. builder = new AlertDialog.Builder(activity);
  10. progressDialog = new ProgressDialog(ctx);
  11. progressDialog.setTitle("Please wait");
  12. progressDialog.setMessage("Connecting to server.....");
  13. progressDialog.setIndeterminate(true);
  14. progressDialog.setCancelable(false);
  15. progressDialog.show();
  16. }
  17.  
  18. @Override
  19. protected String doInBackground(String... params){
  20. String method = params[0];
  21.  
  22. if (method.equals("register"))
  23. {
  24. try {
  25. URL url = new URL(register_url);
  26. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  27. httpURLConnection.setRequestMethod("POST");
  28. httpURLConnection.setDoOutput(true);
  29. httpURLConnection.setDoInput(true);
  30. OutputStream outputStream = httpURLConnection.getOutputStream();
  31. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  32. String email = params[1];
  33. String username = params[2];
  34. String password = params[3];
  35. String data = URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
  36. URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
  37. URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
  38. bufferedWriter.write(data);
  39. bufferedWriter.flush();
  40. bufferedWriter.close();
  41. outputStream.close();
  42. InputStream inputStream = httpURLConnection.getInputStream();
  43. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  44. StringBuilder stringBuilder = new StringBuilder();
  45. String line = "";
  46. while ((line=bufferedReader.readLine())!=null);
  47. {
  48. stringBuilder.append(line+"n");
  49. }
  50. httpURLConnection.disconnect();
  51. Thread.sleep(5000);
  52. return stringBuilder.toString().trim();
  53.  
  54.  
  55. } catch (MalformedURLException e) {
  56. e.printStackTrace();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. } catch (InterruptedException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. return null;
  64. }
  65.  
  66. @Override
  67. protected void onProgressUpdate(Void... values) {
  68. super.onProgressUpdate(values);
  69. }
  70.  
  71. @Override
  72. protected void onPostExecute(String json) {
  73.  
  74. try{
  75. progressDialog.dismiss();
  76.  
  77. JSONObject jsonObject = new JSONObject(json);
  78. JSONArray jsonArray = jsonObject.getJSONArray("server_response");
  79. JSONObject JO = jsonArray.getJSONObject(0);
  80. String code = JO.getString("code");
  81. String message = JO.getString("message");
  82. if (code.equals("reg_true")){
  83. showDialog("Registration Success",message,code);
  84. }
  85. else if(code.equals("reg_false")){
  86. showDialog("Registration Failed!",message,code);
  87. }
  88.  
  89. } catch (JSONException e) {
  90. Log.e("JSON Parser", "Error parsing data:" + e.toString());
  91. }
  92. }
  93.  
  94. public void showDialog(String title,String message,String code){
  95. builder.setTitle(title);
  96. if (code.equals("reg_true")||code.equals("reg_false")){
  97. builder.setMessage(message);
  98. builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  99. @Override
  100. public void onClick(DialogInterface dialog, int which) {
  101. dialog.dismiss();
  102. activity.finish();
  103. }
  104. });
  105. AlertDialog alertDialog = builder.create();
  106. alertDialog.show();
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement