Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.findyou.elsemtim.findyou.SessionFactory;
- import android.accounts.NetworkErrorException;
- import android.os.AsyncTask;
- import android.util.Base64;
- import android.util.Log;
- import java.io.BufferedInputStream;
- import java.io.InputStream;
- import java.net.Authenticator;
- import java.net.HttpURLConnection;
- import java.net.PasswordAuthentication;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Arrays;
- import static com.findyou.elsemtim.findyou.Config.PASSWORD;
- import static com.findyou.elsemtim.findyou.Config.USERNAME;
- class HttpUtils extends AsyncTask<String, Void, Integer> {
- static {
- Authenticator.setDefault(new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("root", "root2".toCharArray());
- }
- });
- }
- @Override
- protected Integer doInBackground(String... params) {
- try {
- HttpURLConnection con = (HttpURLConnection) (new URL("http://10.0.2.2:8080/coordinates")).openConnection();
- //con.setRequestProperty("Authorization", "Basic " + Arrays.toString(Base64.encode("root:root2".getBytes(), Base64.NO_WRAP)) + " ");
- Authenticator.setDefault(new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication("root", "root2".toCharArray());
- }
- });
- con.setDoOutput(true);
- con.setDoInput(true);
- //con.setDoOutput(true);
- con.connect();
- if (con.getResponseCode() != 200) {
- throw new NetworkErrorException(
- String.format("Server returned %d", con.getResponseCode())
- );
- }
- try {
- InputStream in = new BufferedInputStream(con.getInputStream());
- //readStream(in);
- } finally {
- con.disconnect();
- }
- return con.getResponseCode();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement