-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2185 from harytfw/master
- Loading branch information
Showing
16 changed files
with
390 additions
and
33 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
soft1614080902427/java/edu/hzuapps/androidlabs/soft1614080902427/Downloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package edu.hzuapps.androidlabs.soft1614080902427; | ||
|
||
import android.os.AsyncTask; | ||
import android.util.Log; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.io.Reader; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
|
||
public class Downloader { | ||
private final static String TAG = Downloader.class.getSimpleName(); | ||
|
||
private final DownloaderListener listener; | ||
|
||
public Downloader(DownloaderListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
public void createTask(String url) { | ||
|
||
new AsyncTask<String, Integer, ByteArrayOutputStream>() { | ||
public String url; | ||
|
||
@Override | ||
protected ByteArrayOutputStream doInBackground(String[] params) { | ||
this.url = params[0]; | ||
|
||
HttpURLConnection connection = null; | ||
InputStream is = null; | ||
ByteArrayOutputStream out = null; | ||
try { | ||
connection = (HttpURLConnection) new URL(this.url).openConnection(); | ||
is = connection.getInputStream(); | ||
out = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[256]; | ||
int bytesRead; | ||
while((bytesRead = is.read(buffer))!=-1){ | ||
out.write(buffer,0,bytesRead); | ||
} | ||
} catch (Exception e) { | ||
Log.e(TAG,"下载 " +url + "出现错误: "+e.toString()); | ||
} | ||
return out; | ||
} | ||
|
||
@Override | ||
protected void onPreExecute() { | ||
super.onPreExecute(); | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(ByteArrayOutputStream stream) { | ||
super.onPostExecute(stream); | ||
Downloader.this.listener.onDone(this.url, stream); | ||
} | ||
|
||
@Override | ||
protected void onProgressUpdate(Integer... values) { | ||
super.onProgressUpdate(values); | ||
} | ||
|
||
@Override | ||
protected void onCancelled(ByteArrayOutputStream stream) { | ||
super.onCancelled(stream); | ||
listener.onError(this.url); | ||
} | ||
|
||
}.execute(url); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
soft1614080902427/java/edu/hzuapps/androidlabs/soft1614080902427/DownloaderListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package edu.hzuapps.androidlabs.soft1614080902427; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.OutputStream; | ||
|
||
public abstract class DownloaderListener { | ||
public DownloaderListener() { | ||
} | ||
|
||
public void onError(String url) { | ||
|
||
} | ||
|
||
public void onDone(String url, ByteArrayOutputStream stream) { | ||
|
||
} | ||
} |
Oops, something went wrong.