MNM
MNM

Reputation: 2743

Basic authentication android UrlConnection Get

So I am making a test login app so I can use look at how the app and the server are behaving together. So I have three endpoints that I am working with. All three download json file so I can just print that out no problem. The issue is they all require a user name and password. I got the first one to download no problem and it works perfectly. I am using the androids built in authenticator in my httpClass to deal with this. All I have to do is pass my username and password to it and it works just fine.

Another endpoint is going to check if the user can login into the server. This also returns a json. It also requires a username and password. but it will not work with the authenticator i used for the first one. It keeps giving me filenotfound exception and that it. The only want I can get this one to work is to build the url and send it to the httpclass. By manually adding the username and password like this

http://mywebpage/endpoint1

and then add the information like this

http://mywebpage/endpint2

I am doing this manually with code. I do not feel that this is the right way to do this because the first one I got working looks like the right way of doing this. Any suggestions would be appreciated.

This is my html class that I am using

public class MyHttpClass extends AsyncTask<String, Void, String> {

private String user;
private String pass;
Activity act;

//this is used if you need a password and username
//mainly for logins to a webserver
public MyHttpClass(String user, String pass)
{
    this.user = user;
    this.pass = pass;

}

@Override
protected String doInBackground(String... params) {
        String url = params[0];
        return http(url);
}
private String http(String myUrl)

{
    String respone = null;
    HttpURLConnection urlConnection = null;
    try
    {
        URL url = new URL(myUrl);
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, pass.toCharArray());
                }
            });

        urlConnection = (HttpURLConnection)url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();

        if(inputStream != null)
        {
            respone = streamToString(inputStream);
            inputStream.close();
        }

    }catch (IOException ie)
    {
        //ie.printStackTrace();
        Log.d("Issue with Http: " , "IOException");
        ie.printStackTrace();

    }finally {
        if(urlConnection != null)
        {
            urlConnection.disconnect();
        }
    }
    return respone;
}

And this is the main class that I am doing all my testing

public class MainActivity extends AppCompatActivity {

EditText user, pass;
TextView reuslt;

String myJson;
String param1 = "?email=";
String param2 = "&password=";

String email, password;

String customerInfo, loginUrl, promo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    customerInfo = "http://mywebpage/mobileCustomers";
    loginUrl = "http://mywebpage/mobileCustomers/validatePassword";
    promo = "http://a mywebpage/promotions";


    user= (EditText)findViewById(R.id.username);
    pass = (EditText)findViewById(R.id.password);
    reuslt = (TextView)findViewById(R.id.results);
    Button promotion = (Button)findViewById(R.id.button);
    Button crmTest = (Button)findViewById(R.id.user);
    Button loginTest = (Button)findViewById(R.id.login);

    if (promotion != null) {
        promotion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                email = user.getText().toString();
                password = pass.getText().toString();


                Log.d("User:" , email);
                Log.d("Pass: ", password);


                MyHttpGet task = new MyHttpGet(email, password);
                try {
                    myJson = task.execute(promo).get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }catch (NullPointerException np)
                {
                    np.printStackTrace();
                }

                reuslt.setText(myJson);

            }
        });
    }

    if (crmTest != null) {
        crmTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String email = user.getText().toString();
                String password = pass.getText().toString();

                email = param1 + email;
                password = param2 + password;

                Log.d("User:" , email);
                Log.d("Pass: ", password);
                String url = customerInfo+email+password;
                Log.d("Url" , url);
                MyHttpGet task = new MyHttpGet(email, password);
                try {
                    myJson = task.execute(url).get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }catch (NullPointerException np)
                {
                    np.printStackTrace();
                }

                reuslt.setText(myJson);

            }
        });
    }

    if (loginTest != null) {
        loginTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String email = user.getText().toString();
                String password = pass.getText().toString();
                email = param1 + email;
                password = param2 + password;

                Log.d("User:" , email);
                Log.d("Pass: ", password);
                String url = loginUrl+email+password;
                Log.d("Url" , url);

                MyHttpGet task = new MyHttpGet(email, password);
                try {
                    myJson = task.execute(url).get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }catch (NullPointerException np)
                {
                    np.printStackTrace();
                }

                reuslt.setText(myJson);

            }
        });
    }



}

}

Upvotes: 1

Views: 60

Answers (1)

Sairam Rachapudi
Sairam Rachapudi

Reputation: 69

Use Volley library in android. It is used along with GSON to make custom request so that you need not worry about the JSON creation and JSON parsing. Its all very easy.

Have a look at these link http://developer.android.com/training/volley/request-custom.html

Upvotes: 1

Related Questions