A Httputils class for Android
Packagecom.kblw.client04.http;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;/**the tool class for JSON parsing gets the service-side return data and goes to String * Created by KBLW on 2016/5/28.*/ Public classHttputils { Public Staticstring Getjsonstringcontent (String url_path) {Try{URL URL=NewURL (Url_path); HttpURLConnection Connection=(HttpURLConnection) url.openconnection (); Connection.setconnecttimeout (3000); Connection.setrequestmethod ("GET"); Connection.setdoinput (true); intCode =Connection.getresponsecode (); if(code==200){ returnChangeinputstream (Connection.getinputstream ()); } }Catch(Exception e) {e.printstacktrace (); return""; } return""; } Private Staticstring Changeinputstream (InputStream inputstream) {string jsonstring= ""; Bytearrayoutputstream OutputStream=NewBytearrayoutputstream (); intLen = 0; byte[] data =New byte[1024]; Try { while(Len=inputstream.read (data))!=-1) {outputstream.write (data,0, Len); } jsonstring=NewString (Outputstream.tobytearray ()); } Catch(IOException e) {e.printstacktrace (); } returnjsonstring; }}
Let's get this back.
Android's Httputils