Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 #999 #2185

Merged
merged 8 commits into from
May 23, 2018
Merged

#6 #999 #2185

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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