I have an Android app that, once a button is pressed, sends a HTTPRequest to a .php file on my server. Inside the HTTPRequest I store POST variables that is processed server-side.
This is my code:
public void sendRequest(Context context, String url, final HashMap<String, String> hm) {
RequestQueue queue = Volley.newRequestQueue(context);
Log.w("url", url);
// Request a string response from the provided URL.
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.w("Response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.w("Sending progress", "Failure");
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
return new JSONObject(hm).toString().getBytes();
}
@Override
protected Map<String, String> getParams()
{
return hm;
}
};
// Add the request to the RequestQueue.
queue.add(sr);
}
Note that the HashMap variable used is passed into the method itself because I want to use this method dynamically across different other classes. The data gets there okay, but with some glaring issues:
1) For some reason, my JSON String is transmitted as a "key" and not a "value". I actually asked a question about this just now - someone was kind enough to answer me - you can see the question at POST data in PHP formatting issue. Is there a way to solve this?
2) Weirdly enough, when my data reached PHP, all " " and "." characters were replaced with "_". I suspect other sensitive characters would be the same, such as "/" and "'". I understand if this was a GET variable but should this still be happening for POST variables? Or is this related to (1)?
---EDIT---
The HashMap that I'm passing into the method sendRequest
is dynamically generated, but in general it's just:
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("personName", ((EditText) findViewById(R.id.text1).getText().toString().trim());
hm.put("personNumber", ((EditText) findViewById(R.id.text2).getText().toString().trim());
sendRequest(getApplicationContext(), "http://foo.com/bar.php", hm);
The data gets there okay it's just formatted very weirdly and it does have an impact on my processing on the server-side.
String _Url= "put your url here";
//rest request
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest sr = new StringRequest(Request.Method.POST, _Url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response !=null)
{
//you can check the response here by puting log here
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("error", "error: "+error);
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("key1", value in string);
params.put("key2", value in string);
params.put("method", "mehtod name here");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
At first, initialize RequestQueue in onCreate.
RequestQueue queue = Volley.newRequestQueue(this);
Then prepare you variables
Map<String, String> params= new HashMap<>();
params.put("key1", "arg1");
params.put("key2", "arg2");
params.put("key3", "arg3");
And with this void POST query to server-side code.
private void sendRequest(final String QUERY_URL, final Map<String,String> mapParams)
{
StringRequest postRequest = new StringRequest(Request.Method.POST, QUERY_URL ,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Log.d(TAG, response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Log.d(TAG , error.toString());
}
}
) {
@Override
protected Map<String, String> getParams() {
return mapParams;
}
};
queue.add(postRequest);
}
And
sendRequest("http://foo.com/bar.php", params)