Skip to content

Instantly share code, notes, and snippets.

@kenmsj
Forked from mombrea/volley-POST-example.java
Created November 16, 2017 02:37
Show Gist options
  • Select an option

  • Save kenmsj/d333e01a153503ed5abd3ba027f6b19c to your computer and use it in GitHub Desktop.

Select an option

Save kenmsj/d333e01a153503ed5abd3ba027f6b19c to your computer and use it in GitHub Desktop.

Revisions

  1. @mombrea mombrea revised this gist Oct 31, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions volley-POST-example.java
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,10 @@ public Map<String, String> getHeaders() throws AuthFailureError {
    }
    };
    queue.add(sr);
    }

    public interface PostCommentResponseListener {
    public void requestStarted();
    public void requestCompleted();
    public void requestEndedWithError(VolleyError error);
    }
  2. @mombrea mombrea created this gist Oct 31, 2013.
    35 changes: 35 additions & 0 deletions volley-POST-example.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    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);
    }