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