java - Get response body on 400 HTTP response in Android? -
i'm communicating api can not change sends 400 response when request not validated on api side. valid http request, request data not pass application's validation rules.
the 400 response contains json payload has information on why request did not pass validation.
i can't seem response body because httprequestexception thrown. know how retrieve response body?
try { httpurirequest request = params[0]; httpresponse serverresponse = mclient.execute(request); basicresponsehandler handler = new basicresponsehandler(); string response = handler.handleresponse(serverresponse); return response; } catch(httpresponseexception e) { // threw httperror log.d(tag, "httpresponseexception : " + e.getmessage()); log.d(tag, "status code : " + e.getstatuscode()); // todo api returns 400 on successful http payload, invalid user data if(e.getstatuscode() == 400) { // information on api error inside response body } }
this how send , http response byte[]
.of course can change string if want.
byte[] buffer = new byte[1024]; httpclient = new defaulthttpclient(); httppost = new httppost("http://www.rpc.booom.com"); postparameters = new arraylist<namevaluepair>(); postparameters.add(new basicnamevaluepair("debug_data","1")); postparameters.add(new basicnamevaluepair("client_api_ver", "1.0.0.0")); postparameters.add(new basicnamevaluepair("device_identificator", deviceid)); postparameters.add(new basicnamevaluepair("device_resolution", resolution)); postparameters.add(new basicnamevaluepair("username_hash", hashuser(username,password))); postparameters.add(new basicnamevaluepair("password_hash", hashpass(username,password))); httppost.setentity(new urlencodedformentity(postparameters)); httpresponse response = httpclient.execute(httppost); log.w("response ","status line : "+ response.getstatusline().tostring()); buffer = entityutils.tostring(response.getentity()).getbytes();
hope helps!
Comments
Post a Comment