Descarrega.java
1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.upc.pbe.upcnews;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
public class Descarrega extends AsyncTask<URL, Integer, String>
{
final static String TAG = "Descarrega";
private Context ctx;
public Descarrega(Context c)
{
ctx = c;
}
@Override
protected String doInBackground(URL... urls)
{
String html = "";
// Iniciem la connexi� i creem els Streams
try
{
URLConnection conn = urls[0].openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// Iniciem la descarrega i anem guardant al String
Log.d(TAG, "\nDescarregant: \n");
Log.d(TAG, ">> URL: " + urls[0] + " >> Tamany: " + conn.getContentLength() + " bytes");
String inputLine;
while ((inputLine = in.readLine()) != null)
{
html += inputLine + "\n";
}
in.close();
Log.d(TAG, "Descarrega finalitzada");
}
catch(IOException e)
{
this.cancel(true);
Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
return "";
}
return html;
}
}