PDA

Ver la Versión Completa : Leer archivo txt alojado en dropbox


ciberdrac
20/06/15, 19:11:36
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();
}


}

ciberdrac
21/06/15, 14:12:14
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();