How to go about returning the status code from my ServiceHandler (Android JSON) -
i status code each httpresponse , handle code differently each response. using separate servicehandler class below:
public class servicehandler { static string response = null; public final static int = 1; public final static int post = 2; public servicehandler() { } /** * making service call * @url - url make request * @method - http request method * */ public string makeservicecall(string url, int method) { return this.makeservicecall(url, method, null); } /** * making service call * @url - url make request * @method - http request method * @params - http request params * */ public string makeservicecall(string url, int method, list<namevaluepair> params) { try { // http client defaulthttpclient httpclient = new defaulthttpclient(); httpentity httpentity = null; httpresponse httpresponse = null; // checking http request method type if (method == post) { httppost httppost = new httppost(url); // adding post params if (params != null) { httppost.setentity(new urlencodedformentity(params)); } httpresponse = httpclient.execute(httppost); } else if (method == get) { // appending params url if (params != null) { string paramstring = urlencodedutils .format(params, "utf-8"); url += "?" + paramstring; } httpget httpget = new httpget(url); httpresponse = httpclient.execute(httpget); } httpentity = httpresponse.getentity(); response = entityutils.tostring(httpentity); //get status code here int statuscode = httpresponse.getstatusline().getstatuscode(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return response; } } how return statuscode?
here code calls , handles parsing response:
protected void doinbackground(void... arg0) { // creating service handler class instance servicehandler sh = new servicehandler(); // making request url , getting response string result = sh.makeservicecall(url, servicehandler.get); log.d("response: ", "> " + result); if (result != null) { try { notvalid = false; } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); notvalid = true; } return null; } is possible parse status code result above captured in parser?
create class , put both status , response in return class
class myresult{ public string response; public int statuscode; } and code must this:
public class servicehandler { static string response = null; public final static int = 1; public final static int post = 2; public servicehandler() { } /** * making service call * @url - url make request * @method - http request method * */ public myresult makeservicecall(string url, int method) { return this.makeservicecall(url, method, null); } /** * making service call * @url - url make request * @method - http request method * @params - http request params * */ public myresult makeservicecall(string url, int method, list<namevaluepair> params) { myresult result = new myresult(); try { // http client defaulthttpclient httpclient = new defaulthttpclient(); httpentity httpentity = null; httpresponse httpresponse = null; // checking http request method type if (method == post) { httppost httppost = new httppost(url); // adding post params if (params != null) { httppost.setentity(new urlencodedformentity(params)); } httpresponse = httpclient.execute(httppost); } else if (method == get) { // appending params url if (params != null) { string paramstring = urlencodedutils .format(params, "utf-8"); url += "?" + paramstring; } httpget httpget = new httpget(url); httpresponse = httpclient.execute(httpget); } httpentity = httpresponse.getentity(); result.response = entityutils.tostring(httpentity); //get status code here result.statuscode = httpresponse.getstatusline().getstatuscode(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return result; } } and in asynctask:
protected void doinbackground(void... arg0) { // creating service handler class instance servicehandler sh = new servicehandler(); // making request url , getting response myresult result = sh.makeservicecall(url, servicehandler.get); log.d("response: ", "> " + result); if (result.response != null) { try { notvalid = false; } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); notvalid = true; } return null; }
Comments
Post a Comment