Android's Httputils

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.