android - Retrieve the http code of the response in java -
i have following code make post url retrieve response string. i'd http response code (404,503, etc). can recover it? i've tried methods offered httpreponse class didn't find it.
thanks
public static string post(string url, list<basicnamevaluepair> postvalues) { try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); if ((postvalues == null)) { postvalues = new arraylist<basicnamevaluepair>(); } httppost.setentity(new urlencodedformentity(postvalues, "utf-8")); // execute http post request httpresponse response = httpclient.execute(httppost); return requesttostring(response); } catch (exception e) { e.printstacktrace(); return null; } } private static string requesttostring(httpresponse response) { string result = ""; try { inputstream in = response.getentity().getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); stringbuilder str = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { str.append(line + "\n"); } in.close(); result = str.tostring(); } catch (exception ex) { result = "error"; } return result; }
you can modify code this:
//... httpresponse response = httpclient.execute(httppost); if(response.getstatusline().getstatuscode() == httpstatus.sc_ok){ //edit: there function return entityutils.tostring(response.getentity(), "utf-8"); } else { //houston have problem //we should bad http status return null; }
edit: 1 more thing ... instead of requesttostring(..);
can use entityutils.tostring(..);
Comments
Post a Comment