0

So, I'm using Android Studio 1.2.1.1, oKhttp 2.5.0, okio 1.6.0, I have set dependencies correctly (I believe), and I have looked everywhere so i must be doing something very simple incorrectly as no one else seems to have an issue with this.

I have a Java class called OkHttpPostHandler with the following code

import android.os.AsyncTask;

import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;

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

OkHttpClient client = new OkHttpClient();
String varUser, varPass;

public OkHttpPostHandler(String varUser, String varPass) {
    this.varUser = varUser;
    this.varPass = varPass;
}

@Override
protected String doInBackground(String... params){
    RequestBody formBody = new FormEncodingBuilder()
            .add("u", varUser)
            .add("p", varPass)
            .build();
    Request request = new Request.Builder()
            .url(params[0]).post(formBody)
            .build();
    try {
        android.util.Log.w("Test", formBody.toString());
                Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("unexpected code " + response.toString());
        return response.body().string();
    } catch (Exception e) {
    }
    return null;
}

}

And I have a button which reads two EditText views to gain Username and Password, which then fires off the http call:

@Override
public void onClick(View bnLoginSubmit) {
    EditText loginUsernameFont = (EditText) findViewById(R.id.evUsername);
    EditText loginPasswordFont = (EditText) findViewById(R.id.evPassword);

    String varUsername = loginUsernameFont.getEditableText().toString();
    String varPassword = loginPasswordFont.getEditableText().toString();
    String varPasswordHash = BCrypt.hashpw(varPassword, BCrypt.gensalt());

    if (varUsername.isEmpty() || varPassword.isEmpty() || varUsername.contentEquals("") || varPassword.contentEquals("")) {
        Toast.makeText(getApplicationContext(), "Username and/or Password Cannot be Empty",
                Toast.LENGTH_LONG).show();
    } else {
        // sendPostRequest(varUsername, varPasswordHash);
        boolean varPassCheck = BCrypt.checkpw("Password", varPasswordHash);
        OkHttpPostHandler handler = new OkHttpPostHandler("Username_String","Password_String");
        String result = null;
        try {
            result = handler.execute("http://www.example.com").get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        Toast.makeText(getApplicationContext(), "PHP Response = " + result,
                Toast.LENGTH_LONG).show();
    }
}

I assumed the issue was with my bcrypt hashing (so I am using manually entered strings for testing), but that seems to work beautifully. It's just that my parameters are not being passed through. If I enter the parameters manually into the URL it returns correctly, but I would prefer to be able to build on this class so it can be dynamic for future calls.

Can anyone see what I'm doing wrong? It has to be something simple. Any help would be appreciated as I'm struggling to identify the issue.

AranDG
  • 406
  • 4
  • 16
  • Is your backend waiting for a POST request? Could you expose your backend code? – Cristian Oliveira Oct 03 '15 at 18:49
  • Here's a cleansed version of it using just strings – AranDG Oct 03 '15 at 19:02
  • So... there is your problem. In your backend you are expecting a GET request: `$_GET` try to change to $_POST... (I think is it in PHP) – Cristian Oliveira Oct 03 '15 at 19:08
  • But when I change the URL to explicitly include the paramters i.e. www.example.com/login.php?u=Username_String&p=Password_String it works fine, wouldn't this resolve to an error if that was the case? I'll try it anyway and let you know if works, thank you for you swift responses by the way – AranDG Oct 03 '15 at 19:10
  • If I use POST the variables don't get picked up at all, it just returns the variables as null – AranDG Oct 03 '15 at 19:12
  • Added a solution using `$_REQUEST` then you can support both methods. Also, you may like take a look in [php request methods](http://www.w3schools.com/php/php_forms.asp) – Cristian Oliveira Oct 03 '15 at 19:17

1 Answers1

0

The problem is in the backend. It is expecting a GET request.

<?php 
    $username = $_GET['u']; 
    $password = $_GET['p']; 

    if ($username=='Username_String' && $password=='Password_String') { 
        echo 'true'; 
    } else { 
        echo 'username = ' . $username . ' Password = ' . $password; 
    } 
?> 

You can use $_REQUEST to support both GET and POST requests:

<?php 
    $username = $_REQUEST['u']; // or $_POST 
    $password = $_REQUEST['p']; 

    if ($username=='Username_String' && $password=='Password_String') { 
        echo 'true'; 
    } else { 
        echo 'username = ' . $username . ' Password = ' . $password; 
    } 
?>
  • 1
    You are a legend, I knew it would be something simple, just figured it was the client side and not server. Thank you very much for this – AranDG Oct 03 '15 at 19:18