Skip to content

Commit

Permalink
Merge pull request #2185 from harytfw/master
Browse files Browse the repository at this point in the history
  • Loading branch information
zengsn authored May 23, 2018
2 parents d270444 + 884e4aa commit a78d539
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 33 deletions.
Binary file added soft1614080902427/Big Big World.mp3
Binary file not shown.
Binary file added soft1614080902427/I23KW.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion soft1614080902427/Soft1614080902427Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class Soft1614080902427Activity{
public static void main(String[] args){
System.out.println("Hello World!");
System.out.println("Hello World!123");
}
}
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);
}
}
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) {

}
}
Loading

0 comments on commit a78d539

Please sign in to comment.