0

I am trying to do a http post to my server to check if the login credentials are valid with help of this tutorial. I need to make a request to my server but i have to add Authorization, i get the string with the function getB64Auth from this answer. the function logs the right variable, (the same as i use with postman). But for some reason my program stops running if i run my code. I already tried adding the code from the comments but it didn't help.

What am i doing wrong?

private String getB64Auth (String login, String pass) {
    String source=login+":"+pass;
    String ret="Basic "+Base64.encodeToString(source.getBytes(),Base64.URL_SAFE| Base64.NO_WRAP);
    Log.d("authy", ret);
    return ret;
}


/** Called when the user clicks the Login button */
public void login(View view) {

    // Getting username and password
    EditText userText = (EditText) findViewById(R.id.inputLoginUsername);
    EditText passText = (EditText) findViewById(R.id.inputLoginPassword);
    String usernameInput =  userText.getText().toString();
    String passwordInput = passText.getText().toString();

    String authorizationString = getB64Auth(usernameInput,passwordInput );

    // Do something in response to button
    // 1. Create an object of HttpClient
    HttpClient httpClient = new DefaultHttpClient();
    // 2. Create an object of HttpPost

    // I have my real server name down here
    HttpPost httpPost = new HttpPost("https://myservername.com/login");
    // 3. Add POST parameters

    httpPost.setHeader("Authorization", authorizationString);






    // 5. Finally making an HTTP POST request

    try {
        HttpResponse response = httpClient.execute(httpPost);
        Log.d("win", "win1");
        // write response to log
        Log.d("Http Post Response:", response.toString());
    } catch (ClientProtocolException e) {
        Log.d("fail", "Fail 3");
        // Log exception
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("fail", "Fail 4");
        // Log exception
        e.printStackTrace();
    }
}    

When i run my code the app stops working, i can find the log authy but i cant find any fail succes logs. The things i have changed in the example are step 3.

ive added my authorization there.

and removed step 4 cause i dont need it.

Working postman example, with the same request i want to make in android.

enter image description here You can see I get a response, and only set Authorization on my request.

I cant find any decent post/authorization tutorials so i hope i'm looking at the right direction.

It's an android 4.* project

Community
  • 1
  • 1
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • What do you mean by "app stops working" - is it just stuck or completely crashing? Have you tried to sniff the Http traffic with Charles / Fiddler? – Samuil Yanovski Apr 12 '15 at 10:56
  • @SamuilYanovski The app closes and i get the message app stopped – Sven van den Boogaart Apr 12 '15 at 11:01
  • 1
    Do you see anything suspicious in LogCat (i.e. any errors)? A little dummy question - you've included the Internet permission, right? :) – Samuil Yanovski Apr 12 '15 at 11:03
  • Permissions werent set ! added and now it wont crash but i get the following message in my console : 04-12 13:10:11.905 8426-8426/nl.svenboogaart.frietapp D/win﹕ win1 04-12 13:10:11.905 8426-8426/nl.mydomain.myapp D/Http Post Response:﹕ org.apache.http.message.BasicHttpResponse@42491d38 04-12 13:10:11.910 8426-8426/nl.mydomain.myapp I/Choreographer﹕ Skipped 53 frames! The application may be doing too much work on its main thread. – Sven van den Boogaart Apr 12 '15 at 11:13
  • Status code is 200 so its succesful ! – Sven van den Boogaart Apr 12 '15 at 11:16
  • @SamuilYanovski If you add your comment as an answer i wil vote as good answer, String result = EntityUtils.toString(response.getEntity()); Log.d("Http Post Response:", result); gives the result i want as a string ! – Sven van den Boogaart Apr 12 '15 at 11:18

1 Answers1

2

Just a few suggestions about such issues:

  • Check permissions (INTERNET is the one you would need)
  • Applications like Charles / Fiddler let you sniff the Http traffic from the device so you could investigate what is being sent
  • If the application is crashing - check the LogCat messages (for example it could contain a message explaining which permission is missing)

Regarding this message:

The application may be doing too much work on its main thread.

This usually means that you are doing some heavy operations in the main thread - for example parsing the Json from the Http response. Generally you'd like to do all these operations in a background thread and use the main one to update the UI only.

Samuil Yanovski
  • 2,837
  • 17
  • 19