我想要一个示例从网络读取数据并通过意图服务输出到 textview。我阅读了这个http://www.vogella.com/articles/AndroidServices/article.html但下载文件...我想将数据存储为字符串在主要活动的另一个功能中使用。谢谢。
public class DownloadService extends IntentService {
private int result = Activity.RESULT_CANCELED;
public String s = "";
public DownloadService() {
super("DownloadService");
}
// Will be called asynchronously be Android
@Override
protected void onHandleIntent(Intent intent) {
Uri data = intent.getData();
String urlPath = intent.getStringExtra("urlpath");
InputStream stream = null;
//FileOutputStream fos = null;
try {
URL url = new URL(urlPath);
stream = url.openConnection().getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
//fos = new FileOutputStream(output.getPath());
int next = -1;
while ((next = reader.read()) != -1) {
//fos.write(next);
s=s+next;
}
// Sucessful finished
result = Activity.RESULT_OK;