Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. protected String doInBackground(String... params) {
  2. String type = params[0];
  3. String login_url = "https://www.example.com";
  4. if (type.equals("login")) {
  5. try {
  6. String email = params[1];
  7. String password = params[2];
  8. URL url = new URL(login_url);
  9. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  10. httpURLConnection.setDoOutput(true);
  11. httpURLConnection.setDoInput(true);
  12. OutputStream outputStream = httpURLConnection.getOutputStream();
  13. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  14. String post_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&"
  15. + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  16. bufferedWriter.write(post_data);
  17. bufferedWriter.flush();
  18. bufferedWriter.close();
  19. outputStream.close();
  20. InputStream inputStream = httpURLConnection.getInputStream();
  21. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  22. while ((line = bufferedReader.readLine()) != null) {
  23. result += line;
  24. }
  25. bufferedReader.close();
  26. inputStream.close();
  27. httpURLConnection.disconnect();
  28. JSONObject obj= null;
  29. try {
  30. obj = new JSONObject(result);
  31. String status=obj.getString("status");
  32. JSONObject data_obj=obj.getJSONObject("data");
  33. win_user=data_obj.getString("id");
  34. name=data_obj.getString("name");
  35. logo=data_obj.getString("logo");
  36.  
  37. return win_user;
  38. } catch (JSONException e) {
  39. e.printStackTrace();
  40. }
  41. return result;
  42.  
  43.  
  44. } catch (MalformedURLException e) {
  45. e.printStackTrace();
  46. } catch (IOException e) {
  47. e.printStackTrace();}
  48. }
  49. return null;
  50. }
  51.  
  52. @Override
  53. protected void onPreExecute() {
  54. alertDialog = new AlertDialog.Builder(context).create();
  55. alertDialog.setTitle("Login Status");
  56. }
  57.  
  58.  
  59. @Override
  60. protected void onPostExecute(String result) {
  61. if (result == null ) {
  62. Toast pass = Toast.makeText(context, "Email or Password is not match!", Toast.LENGTH_SHORT);
  63. pass.show();
  64. }
  65. else
  66. {
  67. context.startActivity( new Intent(context,Main.class));
  68. Main.JSON_STRING = result;
  69. Main.name = name;
  70. Main.logo = logo;
  71. }
  72.  
  73. }
  74.  
  75. @Override
  76. protected void onProgressUpdate(Void... values) {
  77. super.onProgressUpdate(values);
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement