android - InputStream.available() is always 0 -
i trying connect server android app , json response. using httpurlconnection
to ping server. server running fine since getting response on browser. in code below, inputstream.available()
returns 0 , hence cannot parse data. there way response ?
note : below method called inside separate thread (asynctask<void, void, void>
)
private void pingserver(final string serverurl) throws ioexception { inputstream inputstream = null; try { final url url = new url(serverurl); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setreadtimeout(10000);// milliseconds conn.setconnecttimeout(15000);// milliseconds conn.setrequestmethod("get"); conn.setdoinput(true); conn.connect(); inputstream = conn.getinputstream(); string resp = readit(inputstream, inputstream.available());//resp empty :( } catch (ioexception e) { } { if (inputstream != null) { inputstream.close(); } } } private string readit(final inputstream stream,final int len) throws ioexception { final reader reader = new inputstreamreader(stream, "utf-8"); final char[] buffer = new char[len]; reader.read(buffer); return new string(buffer); }
Comments
Post a Comment