Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. public class BackgroundWorker extends AsyncTask {
  2. Context context;
  3. BackgroundWorker (Context context){
  4. this.context=context;
  5. }
  6.  
  7. @Override
  8. protected Object doInBackground(Object[] objects) {
  9. Object type=objects[0];
  10. String login_url = "http://localhost/android/user.php";
  11. if(type.equals("login")){
  12. try{
  13.  
  14. String username= (String) objects[1];
  15. String password=(String)objects[2];
  16. URL url=new URL(login_url);
  17. HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
  18. httpURLConnection.setRequestMethod("POST");
  19. httpURLConnection.setDoOutput(true);
  20. httpURLConnection.setDoInput(true);
  21. OutputStream outputStream=httpURLConnection.getOutputStream();
  22. BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  23. String post_data= URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
  24. +URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8");
  25. bufferedWriter.write(post_data);
  26. bufferedWriter.flush();
  27. bufferedWriter.close();
  28. outputStream.close();
  29. InputStream inputStream=httpURLConnection.getInputStream();
  30. BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  31. String result="";
  32. String line="";
  33. while(bufferedReader.readLine()!=null){
  34. result+=line;
  35. }
  36. bufferedReader.close();
  37. inputStream.close();
  38. httpURLConnection.disconnect();
  39. return result;
  40. }
  41. catch(MalformedURLException e){
  42. e.printStackTrace();
  43. }
  44. catch (IOException e){
  45. e.printStackTrace();
  46. }
  47. }
  48. return null;
  49. }
  50. }
Add Comment
Please, Sign In to add comment