How to use POST request in android Volley library with params and header? -
i trying learn volley library posting data webservices. need implement user registration form, following image of postman parameters , header...
now problem is, getting below error
com.android.volley.servererror
this code volley post method.
public void postnewcomment(){ try { requestqueue requestqueue = volley.newrequestqueue(this); string url = "http://myurl/api/users"; jsonobject jsonbody = new jsonobject(); jsonbody.put("email", "test1@gmail.com"); jsonbody.put("user_type", "c"); jsonbody.put("company_id", "0"); jsonbody.put("status", "a"); jsonbody.put("password", "123456"); final string requestbody = jsonbody.tostring(); stringrequest stringrequest = new stringrequest(request.method.post, url, new response.listener<string>() { @override public void onresponse(string response) { log.i("volley", response); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { error.printstacktrace(); log.e("volley", error.tostring()); } }) { @override public string getbodycontenttype() { return "application/json; charset=utf-8"; } @override public map<string, string> getheaders() throws authfailureerror { final map<string, string> headers = new hashmap<>(); headers.put("authorization", "basic " + "my_auth_key"); headers.put("content-type", "application/json"); return headers; } @override protected response<string> parsenetworkresponse(networkresponse response) { string responsestring = ""; if (response != null) { responsestring = string.valueof(response.statuscode); // can more details such response.headers } return response.success(responsestring, httpheaderparser.parsecacheheaders(response)); } }; requestqueue.add(stringrequest); } catch (jsonexception e) { e.printstacktrace(); } }
please suggest getting wrong. url working correct postman, can see need set 2 headers. tried url post method asynctask , working good. need implement using volley library. kindly suggest. thank you.
this logcat error:
e/volley: [81910] basicnetwork.performrequest: unexpected response code 405 "myurl"
**try 1 **
private void sendworkpostrequest() { try { string url = ""; jsonobject jsonbody = new jsonobject(); jsonbody.put("email", "abc@abc.com"); jsonbody.put("password", ""); jsonbody.put("user_type", ""); jsonbody.put("company_id", ""); jsonbody.put("status", ""); jsonobjectrequest jsonoblect = new jsonobjectrequest(request.method.post, url, jsonbody, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { toast.maketext(getapplicationcontext(), "response: " + response.tostring(), toast.length_short).show(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { onbackpressed(); } }) { @override public map<string, string> getheaders() throws authfailureerror { final map<string, string> headers = new hashmap<>(); headers.put("authorization", "basic " + "c2fnyxjaa2fydhbhes5jb206cnmwm2uxqup5rnqznkq5ndbxbjnmudgznve3staynzi=");//put token here return headers; } }; volleyapplication.getinstance().addtorequestqueue(jsonoblect); } catch (jsonexception e) { e.printstacktrace(); } // toast.maketext(getapplicationcontext(), "done", toast.length_long).show(); } }
Comments
Post a Comment