Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of performing a POST request using Google Volley for Android
public static void postNewComment(Context context,final UserAccount userAccount,final String comment,final int blogId,final int postId){
mPostCommentResponse.requestStarted();
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest sr = new StringRequest(Request.Method.POST,"http://api.someservice.com/post/comment", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mPostCommentResponse.requestCompleted();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mPostCommentResponse.requestEndedWithError(error);
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user",userAccount.getUsername());
params.put("pass",userAccount.getPassword());
params.put("comment", Uri.encode(comment));
params.put("comment_post_ID",String.valueOf(postId));
params.put("blogId",String.valueOf(blogId));
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);
}
public interface PostCommentResponseListener {
public void requestStarted();
public void requestCompleted();
public void requestEndedWithError(VolleyError error);
}
@bfaulk96

This comment has been minimized.

Copy link

@bfaulk96 bfaulk96 commented Jun 15, 2015

I'm fairly new to everything about creating apps, and was curious where the mPostCommentResponse came from.

@chakermallekiac

This comment has been minimized.

Copy link

@chakermallekiac chakermallekiac commented Jun 23, 2015

@bfaulk96 delete those methods (mPostCommentResponse,..) you do not need them.

@ChongDeng

This comment has been minimized.

Copy link

@ChongDeng ChongDeng commented Dec 28, 2015

how to post a form, and get a json from server

@vikash010890

This comment has been minimized.

Copy link

@vikash010890 vikash010890 commented Jan 7, 2016

And what is mPostCommentResponse ?

@samsantosh

This comment has been minimized.

Copy link

@samsantosh samsantosh commented Jul 5, 2016

getting this response for Post request "BasicNetwork.performRequest: Unexpected response code 422 for http......

@visionsol

This comment has been minimized.

Copy link

@visionsol visionsol commented Jul 22, 2016

Hey there,
@mombrea can you please post "http://api.someservice.com/post/comment" sample php code here? I want to see how parameters passed with params.put() method handled in php. you can mail file on "tallydeveloper@visionsol.in"

@Janipasha

This comment has been minimized.

Copy link

@Janipasha Janipasha commented Apr 19, 2017

How can you pass the string response to main-activity ? Please let me know

@prabinshrestha

This comment has been minimized.

Copy link

@prabinshrestha prabinshrestha commented Jun 28, 2017

Can we do the same with JSONObjectRequest

@hackaprende

This comment has been minimized.

Copy link

@hackaprende hackaprende commented Oct 26, 2017

What about sending an array of objects, like a JSONArray when you post a JSONObject?

@KingsleyUsoroeno

This comment has been minimized.

Copy link

@KingsleyUsoroeno KingsleyUsoroeno commented Jan 12, 2019

I have a question why are we starting with a StringResquest is it based on the Api you are using or what and what if you would love to get a request back from the server how would you go about doing that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment