|
||
|
![]() |
![]() |
Programación y Desarrollo para Android Subforo exclusivo para temas de programación de software para PDAs y desarrollo de aplicaciones, interfaces, etc bajo Android |
![]() |
|
Herramientas |
#1
|
||||
|
||||
Leer archivo txt alojado en dropbox
Después de varios días probando, no consigo leer un archivo txt que tengo alojado en el dropbox, consultando por la red he recopilado el siguiente código, pero la aplicación se detiene, a ver si alguien puede decirme dónde está el error y me saca del atasco.
public class MainActivity extends ActionBarActivity { @override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String contents = ""; TextView t = (TextView) findViewById(R.id.TvProba); t.setText("probando"); try { URLConnection conn = new URL("https://www.dropbox.com/s/g0fythqeht80e53/version.txt?dl=0").openConnection(); InputStream in = conn.getInputStream(); contents = readStream(in); } catch (MalformedURLException e) { Log.w("", "MALFORMED URL EXCEPTION"); } catch (IOException e) { Log.w(e.getMessage(), e); } t.setText( contents ); } public String readStream(InputStream in) throws IOException { BufferedReader r = null; r = new BufferedReader(new InputStreamReader(in)); StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line); } if (r != null) { r.close(); } in.close(); return total.toString(); } } |
|
#2
|
||||
|
||||
Bueno, finalmente he conseguido leer un archivo de un servidor remoto, aquí abajo os dejo el código, lo que pasa es que no me sirve, por que resulta que el archivo al que quiero acceder está en un servidor seguro, del tipo https, para ese tipo de acceso con este código no me vale, a ver si hay alguien que pueda orientarme como puedo utilizar el HttpsUrlConnection.
Thread leerarchivo = new Thread(){ public void run() { try { String contents = ""; TextView t = (TextView) findViewById(R.id.TvProba); t.setText("probando"); URLConnection conn = new URL("http://www.LinkDelArchivo").openConnection(); InputStream in = conn.getInputStream(); contents = readStream(in); t.setText(contents); } catch (MalformedURLException e) { Log.w("", "MALFORMED URL EXCEPTION"); } catch (IOException e) { Log.w(e.getMessage(), e); } } }; leerarchivo.start(); } public String readStream(InputStream in) throws IOException { BufferedReader r = null; r = new BufferedReader(new InputStreamReader(in)); StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line); } if (r != null) { r.close(); } in.close(); return total.toString(); |
![]() |
![]() |
||||||
|