Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. public static String main(String x, String y){
  2. if (android.os.Build.VERSION.SDK_INT > 9)
  3. {
  4. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  5. StrictMode.setThreadPolicy(policy);
  6.  
  7. }else{
  8.  
  9. try{
  10.  
  11. String username = x;
  12. String password = y;
  13.  
  14. URL url = new URL(login_url);
  15. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  16. httpURLConnection.setRequestMethod("POST");
  17. httpURLConnection.setDoInput(true);
  18. httpURLConnection.setDoOutput(true);
  19.  
  20. OutputStream outputStream = httpURLConnection.getOutputStream();
  21. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  22.  
  23. String post_data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
  24. +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
  25.  
  26. bufferedWriter.write(post_data);
  27. bufferedWriter.flush();
  28. bufferedWriter.close();
  29. outputStream.close();
  30.  
  31. // LÄSER FRÅN HEMSIDAN
  32.  
  33. InputStream inputStream = httpURLConnection.getInputStream();
  34. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  35. String result="";
  36. String line="";
  37. while((line = bufferedReader.readLine()) != null){
  38.  
  39. result += line;
  40. }
  41.  
  42.  
  43.  
  44. //AVSLUTA CONNECTION
  45. bufferedReader.close();
  46. inputStream.close();
  47. httpURLConnection.disconnect();
  48.  
  49. return result;
  50.  
  51. }catch(Exception e){
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56.  
  57.  
  58. return null;
  59.  
  60. }
  61.  
  62. <uses-permission android:name="android.permission.INTERNET" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement