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.