DownloadAsynchActivity.java
Dosyayı İndir
package com.godoro.androidclients;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class DownloadAsynchActivity extends Activity {
private TextView messageView;
private TextView textView;
private String remoteAddress="http://www.godoro.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_asynch);
messageView=(TextView) findViewById(R.id.messageView);
textView=(TextView) findViewById(R.id.textView);
}
public void onClickDownloadAsynch(View view){
DownloadTextTask task=new DownloadTextTask();
task.execute(remoteAddress);
}
private class DownloadTextTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... address) {
try {
return DownloadAsynchActivity.get(address[0]);
}catch(Exception e){
Log.e("GodoroAndroid","Eşansız indirme yanlışı",e);
return null;
}
}
@Override
protected void onPostExecute(String text) {
showText(text);
}
}
private void showText(String text){
textView.setText(text);
messageView.setText(R.string.textDownloaded);
}
private static String get(String address) throws IOException{
URL url = new URL(address);
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
String content = read(in);
return content;
}
private static String read(InputStream in)
throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\r\n");
}
in.close();
return builder.toString();
}
}
Dosyayı İndir