Http - Baixar arquivo texto |
Top Previous Next |
Tela BaixarActivity.java
package com.baixar;
import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;
public class BaixarActivity extends Activity implements OnClickListener, Runnable {
// handler usado para atualizar a view private Handler handler = new Handler(); private ProgressDialog dialog;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
Button btBaixar = (Button) findViewById(R.id.btBaixar); btBaixar.setOnClickListener(this); }
public void onClick(View arg0) {
dialog = ProgressDialog.show(this, "Exemplo", "Buscando texto, aguarde...", false, true); // faz o download numa thread new Thread(this).start(); }
public void run() {
try { final String nome_arquivo = "http://market.com.br/arquivo.txt"; Http http = new Http(); final String texto = http.downloadArquivo(nome_arquivo); // precisa do handler para atualizar a view de outra thread handler.post(new Runnable() {
public void run() { TextView tvResultado = (TextView) findViewById(R.id.tvRetorno); tvResultado.setText(texto); } }); } catch (Throwable e) { Log.e("Erro", e.getMessage(), e); } finally { dialog.dismiss(); } } }
main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<Button android:id="@+id/btBaixar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Baixar arquivo texto" />
<TextView android:id="@+id/tvRetorno" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Retorno" />
</LinearLayout>
Http.java
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;
import android.util.Log;
public class Http {
public final String downloadArquivo(String url) { Log.i("TESTE", "Http.Download: " + url); try { URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); // configura get conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setDoOutput(false); conn.connect(); InputStream in = conn.getInputStream(); // arquivo String arquivo = readString(in); conn.disconnect(); return arquivo; } catch (Exception e) { Log.e("Erro", e.getMessage()); } return null; }
private byte[] readBytes(InputStream in) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { bos.write(buffer, 0, len); }
byte[] bytes = bos.toByteArray(); return bytes; } finally { bos.close(); in.close(); } }
private String readString(InputStream in) throws IOException { byte[] bytes = readBytes(in); String texto = new String(bytes); Log.e("Retorno", texto); return texto; } }
Manifest <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.baixar" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".BaixarActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest> |