android - Getting BasicNetwork.performRequest: Unexpected response code 400 in Volley -
in activity checks user credentials , returns session id , related info if valid. method post.the parameter has send json.
{ "params": { "context": {}, "db": "testing", "login": "admin", "password": "admin" } } so create jsonobject , send header.i getting response in postman.but m getting error when call is.can 1 me in this?
private void volleylogin() throws jsonexception { mprogressview.setvisibility(view.visible); jsonobject 1 = new jsonobject(); one.put("context",new jsonobject()); one.put("db","testing"); one.put("login","admin"); one.put("password","admin"); jsonobject params = new jsonobject(); params.put("params",one); hashmap<string, string> header = new hashmap<string, string>(); header.put("content-type", "application/json; charset=utf-8"); requestqueue requestqueue = volley.newrequestqueue(this); customrequest jsobjrequest = new customrequest(request.method.post, apiconstants.url_authenticate,params,header, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { system.out.println("response"+response); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { system.out.println("volleyerror"+error); } } ); jsobjrequest.setretrypolicy(new defaultretrypolicy( (int) timeunit.seconds.tomillis(120), defaultretrypolicy.default_max_retries, defaultretrypolicy.default_backoff_mult)); system.out.println("jsobjrequest"+jsobjrequest); requestqueue.add(jsobjrequest); } here custom request class
public class customrequest extends request<jsonobject> { private response.listener<jsonobject> listener; private jsonobject jsonobjectparams; private map<string, string> headers; public customrequest(int method,string url, jsonobject jsonobjectparams,map<string, string> headers, response.listener<jsonobject> reponselistener, response.errorlistener errorlistener) { super(method, url, errorlistener); this.listener = reponselistener; this.jsonobjectparams = jsonobjectparams; this.headers= headers; system.out.println("method"+method); system.out.println("url"+url); system.out.println("jsonobjectparams"+jsonobjectparams); } @override public map<string, string> getheaders() throws authfailureerror { return headers; } @override protected response<jsonobject> parsenetworkresponse(networkresponse response) { try { string jsonstring = new string(response.data, httpheaderparser.parsecharset(response.headers)); return response.success(new jsonobject(jsonstring), httpheaderparser.parsecacheheaders(response)); } catch (unsupportedencodingexception e) { return response.error(new parseerror(e)); } catch (jsonexception je) { return response.error(new parseerror(je)); } } @override protected void deliverresponse(jsonobject response) { // todo auto-generated method stub listener.onresponse(response); } @override protected volleyerror parsenetworkerror(volleyerror volleyerror) { if(volleyerror.networkresponse != null && volleyerror.networkresponse.data != null){ volleyerror error = new volleyerror(new string(volleyerror.networkresponse.data)); volleyerror = error; } return volleyerror; } }
you can try this:
create singletone class volleydispatcher, holds requestqueue volley.
public requestqueue getrequestqueue() { if (requestqueue == null) { // getapplicationcontext() key, keeps leaking // activity or broadcastreceiver if passes 1 in. requestqueue = volley.newrequestqueue(mcontext.getapplicationcontext()); } return requestqueue; } /** * recreates request queue using username/password https auth. */ public void recreaterequestqueue() { if (!apputil.isempty(appconstants.username) && !apputil.isempty(appconstants.password)) {//check if user logged in https auth can created if necessary if (requestqueue != null) { requestqueue.stop(); requestqueue = null; } requestqueue = volley.newrequestqueue(mcontext.getapplicationcontext(), new hurlcustomstack()); } } public <t> void addtorequestqueue(request<t> req) { getrequestqueue().add(req); } call recreaterequestqueue before doing volley requests
implement custom hurlstack
public class hurlcustomstack extends hurlstack { @override protected httpurlconnection createconnection(url url) throws ioexception { // workaround m release httpurlconnection not observing // httpurlconnection.setfollowredirects() property. // https://code.google.com/p/android/issues/detail?id=194495 // connection.setinstancefollowredirects(httpurlconnection.getfollowredirects()); return httpurlconnectionhelper.getinstance().createhttpurlconnection(url); } } and client creation method:
public httpurlconnection createhttpurlconnection(url url) throws ioexception { log.d(tag, "create http url connection url : " + url.tostring()); httpurlconnection httpconnection = null; if ("https".equalsignorecase(url.getprotocol())) { httpsurlconnection https = (httpsurlconnection) url.openconnection(); https.sethostnameverifier(do_not_verify); httpconnection = https; } else { httpconnection = (httpurlconnection) url.openconnection(); } httpconnection.setreadtimeout(timeout); httpconnection.setconnecttimeout(timeout); string basicauth = "basic " + new string(base64.encode((appconstants.username + ":" + appconstants.password).getbytes(), base64.no_wrap)); httpconnection.setrequestproperty("authorization", basicauth); httpconnection.setrequestproperty("accept-language", locale.getdefault().getlanguage()); return httpconnection; } so now, every time want make request, use addtorequestqueue method in volleydispatcher class.
Comments
Post a Comment