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.