I've spent a few days trying to figure out volley & finally managed to find out ways to use the Method.POST. I'm using StringRequest to pass email_address parameter to my PHP. Now it's returning a JSON string:
{"user_id":1,"email_address":"adminuser"}
However, I couldn't extract data using the normal getString() method. It's giving me "no value" error. Here's my code. Just an extract of it:
// Login button
btnLogin = (Button) findViewById(R.id.btnLogin);
// Login button click event
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String param = txtUsername.getText().toString();
postData(param);
}
});
}
/**
* Posting parameter
* */
private void postData(final String param) {
// Get username, password from EditText
String username = txtUsername.getText().toString();
String password = txtPassword.getText().toString();
// Check if username, password is filled
if(username.trim().length() > 0 && password.trim().length() > 0){
if(username.equals("adminuser") && password.equals("test")){
RequestQueue rq = Volley.newRequestQueue(this);
StringRequest postReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
try {
JSONObject person = new JSONObject();
String name = person.getString("user_id");
String email = person.getString("email_address");
//Store session
session.createLoginSession(name, email);
// Staring MainActivity
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
//}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("email_address", param);
return params;
}
};
rq.add(postReq);
}
}
}
I have a feeling it's because the response is returned as String (I could be wrong). But I've tried using custom request, gson but whatever I did using post method is not working. The StringRequest is the only method that actually return something but now, I couldn't extract data from it. I want to store the data in session.
It'd be great if anyone could help as I have googled & tried whatever solutions I could find but to no avail. Thanks!
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
try {
//Do it with this it will work
JSONObject person = new JSONObject(response);
String name = person.getString("user_id");
String email = person.getString("email_address");
//Store session
session.createLoginSession(name, email);
// Staring MainActivity
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
//}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With