android - Return response data from a web service using java -
i building android application need use web service post data , return string. achieve have created asynctask
in background.
protected string doinbackground(void... params) { url posturl; try { posturl = new url("http://192.168.2.102/rest%20service/index.php"); } catch (malformedurlexception e) { throw new illegalargumentexception("invalid url"); } string body = "mdxemail=" + email + "&mdxpassword="+ password; byte[] bytes = body.getbytes(); string response = null; httpurlconnection conn = null; try { conn = (httpurlconnection) posturl.openconnection(); conn.setdooutput(true); conn.setusecaches(false); conn.setfixedlengthstreamingmode(bytes.length); conn.setrequestmethod("post"); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded;charset=utf-8"); // post request outputstream out = conn.getoutputstream(); out.write(bytes); out.close(); // handle response int status = conn.getresponsecode(); if (status != 200) { throw new ioexception("post failed error code " + status); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } { if (conn != null) { conn.disconnect(); } } return response; }
my question how can data returned web service?
once posted can returned data this:
bufferedreader in = new bufferedreader( new inputstreamreader(conn.getinputstream())); string inputline; stringbuffer response = new stringbuffer(); while ((inputline = in.readline()) != null) { response.append(inputline); } in.close();
Comments
Post a Comment