Reputation: 31
AnyBody, please solve my issue. I am unable to get any POST values from volley StringRequest.What is the Problem in my code
Here is my Android Code
StringRequest stringRequest = new StringRequest(Request.Method.POST,URL,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Log.d("response",response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(PostRequestTest.this, error.toString(), Toast.LENGTH_LONG).show();
}
})
{
//getaParams function is used to send the values to the server
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
params.put("user_name","uname");
Log.d("parameters09876", params.toString());
JSONObject jsonObject = new JSONObject(params);
Log.d("jsonRequest",jsonObject.toString());
return params;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
Config.MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
CustomVolleyRequest.getInstance(this).addToRequestQueue(stringRequest);
}
And in my PHP Code, i simply Print the POST value
<?php echo $_POST['user_name']; ?>
Here is my CustomVolleyRequest
public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private CustomVolleyRequest(Context context) {
this.context = context ;
this.requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req)
{
getRequestQueue().add(req);
}
public ImageLoader getImageLoader()
{
return imageLoader;
}
}
Upvotes: 1
Views: 564
Reputation: 570
I give one suggestion to you. use JsonObjectRequest
instead of using StringRequest
.
StringRequest
: Specify a URL and receive a raw string in response.
JsonObjectRequest
and JsonArrayRequest
(both subclasses of JsonRequest). Specify a URL and get a JSON object or array (respectively) in response.
TextView mTxtDisplay;
ImageView mImageView;
mTxtDisplay = (TextView) findViewById(R.id.txtDisplay);
String url = "http://my-json-feed";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mTxtDisplay.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
Upvotes: 1