Advertisement
elsemTim

Untitled

Jun 3rd, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package com.findyou.elsemtim.findyou.SessionFactory;
  2.  
  3. import android.accounts.NetworkErrorException;
  4. import android.os.AsyncTask;
  5. import android.util.Base64;
  6. import android.util.Log;
  7.  
  8. import java.io.BufferedInputStream;
  9. import java.io.InputStream;
  10. import java.net.Authenticator;
  11. import java.net.HttpURLConnection;
  12. import java.net.PasswordAuthentication;
  13. import java.net.URL;
  14. import java.net.URLConnection;
  15. import java.util.Arrays;
  16.  
  17. import static com.findyou.elsemtim.findyou.Config.PASSWORD;
  18. import static com.findyou.elsemtim.findyou.Config.USERNAME;
  19.  
  20. class HttpUtils extends AsyncTask<String, Void, Integer> {
  21.     static {
  22.         Authenticator.setDefault(new Authenticator() {
  23.             protected PasswordAuthentication getPasswordAuthentication() {
  24.                 return new PasswordAuthentication("root", "root2".toCharArray());
  25.             }
  26.         });
  27.     }
  28.  
  29.     @Override
  30.     protected Integer doInBackground(String... params) {
  31.  
  32.  
  33.         try {
  34.             HttpURLConnection con = (HttpURLConnection) (new URL("http://10.0.2.2:8080/coordinates")).openConnection();
  35.             //con.setRequestProperty("Authorization", "Basic " + Arrays.toString(Base64.encode("root:root2".getBytes(), Base64.NO_WRAP)) + " ");
  36.             Authenticator.setDefault(new Authenticator() {
  37.                 @Override
  38.                 protected PasswordAuthentication getPasswordAuthentication() {
  39.                     return new PasswordAuthentication("root", "root2".toCharArray());
  40.                 }
  41.             });
  42.             con.setDoOutput(true);
  43.             con.setDoInput(true);
  44.             //con.setDoOutput(true);
  45.  
  46.             con.connect();
  47.             if (con.getResponseCode() != 200) {
  48.                 throw new NetworkErrorException(
  49.                         String.format("Server returned %d", con.getResponseCode())
  50.                 );
  51.             }
  52.  
  53.             try {
  54.                 InputStream in = new BufferedInputStream(con.getInputStream());
  55.                 //readStream(in);
  56.             } finally {
  57.                 con.disconnect();
  58.             }
  59.  
  60.             return con.getResponseCode();
  61.         } catch (Exception e) {
  62.             e.printStackTrace();
  63.             return null;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement