java - android-Volley JSONObjectRequest return 401 error -
i'm trying send post request paramteres server. post params null.
i've tried few solutions stackoverflow
didn't work.
i unexpected response code 401 11.urlname
map<string, string> jsonparams = new hashmap<string, string>(); jsonparams.put("username", "test@mail.com"); jsonparams.put("usertype", "usertype"); jsonparams.put("apikey", "key"); jsonobjectrequest myrequest = new jsonobjectrequest(request.method.post,apiurl, new jsonobject(jsonparams), new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { try { string status=response.getstring("status"); if (status.equals("success")) { txtresponse.settext("valid user"); } else { txtresponse.settext("invalid user"); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }) { @override public map<string, string> getheaders() throws authfailureerror { hashmap<string, string> headers = new hashmap<string, string>(); headers.put("content-type", "application/json; charset=utf-8"); return headers; } }; appcontroller.getinstance().addtorequestqueue(myrequest, "tag");
my app controller code is
public class appcontroller extends application { public static final string tag = appcontroller.class.getsimplename(); private requestqueue mrequestqueue; private static appcontroller minstance; @override public void oncreate() { super.oncreate(); minstance = this; } public static synchronized appcontroller getinstance() { return minstance; } public requestqueue getrequestqueue() { if (mrequestqueue == null) { mrequestqueue = volley.newrequestqueue(getapplicationcontext()); } return mrequestqueue; } public <t> void addtorequestqueue(request<t> req, string tag) { req.settag(textutils.isempty(tag) ? tag : tag); getrequestqueue().add(req); } public <t> void addtorequestqueue(request<t> req) { req.settag(tag); getrequestqueue().add(req); } public void cancelpendingrequests(object tag) { if (mrequestqueue != null) { mrequestqueue.cancelall(tag); } } }
if issue has not been solved, can refer following code build request body (params)
private string buildrequestbody(object content) { string output = null; if ((content instanceof string) || (content instanceof jsonobject) || (content instanceof jsonarray)) { output = content.tostring(); } else if (content instanceof map) { uri.builder builder = new uri.builder(); hashmap hashmap = (hashmap) content; if (isvalid(hashmap)) { iterator entries = hashmap.entryset().iterator(); while (entries.hasnext()) { map.entry entry = (map.entry) entries.next(); builder.appendqueryparameter(entry.getkey().tostring(), entry.getvalue().tostring()); entries.remove(); // avoids concurrentmodificationexception } output = builder.build().getencodedquery(); } } return output; }
then...
map<string, string> stringmap = new hashmap<>(); stringmap.put("username", "yourusername"); stringmap.put("password", "yourpassword"); ... string requestbody = buildrequestbody(stringmap);
Comments
Post a Comment