Andriod file download with server side (Kit httputils) II

Source: Internet
Author: User

Also on the basis of the previous server, the client also in the login interface andriod simple HTTP GET request on the basis of user registration to jump to download interface, this article download interface only two view, one is textview display registered user name (this article does not do login interface, The method is similar to registration, except that the user name is queried in the database on the server side and the password is correct. The other is the download button, which you click to download to the SD card.

The toolkit, which encapsulates the HTTP request, uses the Get method and uses the HttpURLConnection class to take care of the specific request.

Adding Senddownloadpost methods to the Httputils class

The specific code is as follows:

public static void Senddownloadpost (URL URLs) {inputstream inputstream=null;//string path= "http://192.168.0.179:8080/ Myweb/download.do "; OutputStream outputstream=null;try {//url = new URL;//This article uses HttpURLConnection, HttpClient can httpurlconnection connection= (httpurlconnection) urls.openconnection (); Connection.setrequestmethod ("GET");//timeout request set to 3sconnection.setconnecttimeout (3000);//Set Response time 10sconnection.setreadtimeout (10000); Connection.setdoinput (True); Connection.setdooutput (true);//Get Return code int responsecode=connection.getresponsecode (); /request correct if (responsecode==200) {log.d (TAG, "return correct!! "); Inputstream=new Bufferedinputstream (Connection.getinputstream ());//generate SD card files path file File=new ( Environment.getexternalstoragedirectory () +file.separator + "A.pdf"); outputstream=new BufferedOutputStream (new FileOutputStream (file)); byte[] str=new byte[2048];int len=-1;if (Environment.MEDIA_MOUNTED.equals ( Environment.getexternalstoragestate ())) {LOG.D (TAG, "have permission");//writes Inpustream to SD card while ((Len=inputstream.read (str))! =-1) {outputstream.write (str, 0, Len);}}}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{if (inputstream!=null) {try {inputstream.close ()} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} if (outputstream!=null) {try {outputstream.close ()} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} return;}

Friendly tips: This article needs to add permissions: Internet access, SD card file read and Write permissions, SD card file creation rights

Specifically in Manifest.xml

Add the following:

<uses-permission android:name= "Android.permission.INTERNET" ></uses-permission>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission>

Andriod file download with server side (Kit httputils) II

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.