Feelix333
Stammgast
- 317
Hallo zusammen,
Wie der Titel schon sagt, möchte ich einen einfachen Text aus einer txt-Datei, die auf einem Server liegt auslesen und wiedergeben.
Ich habe jetzt schon viele Ansätze probiert, aber komme einfach nicht weiter - oft widersprechen sich Aussagen in stackoverflow auch..
Ich habe vom Layout her einen einfachen Button und einen TextView, beim klick auf den Button soll der Text ausgelesen werden und in der TextView angezeigt werden.
Variante 1
Variante 2
Variante 3
Das ganze kann doch nicht so schwer sein?!
Bitte helft mir, bin langsam echt frustriert -.-
mfg
Felix
Wie der Titel schon sagt, möchte ich einen einfachen Text aus einer txt-Datei, die auf einem Server liegt auslesen und wiedergeben.
Ich habe jetzt schon viele Ansätze probiert, aber komme einfach nicht weiter - oft widersprechen sich Aussagen in stackoverflow auch..
Ich habe vom Layout her einen einfachen Button und einen TextView, beim klick auf den Button soll der Text ausgelesen werden und in der TextView angezeigt werden.
Variante 1
Code:
public void onClick(View v) {
if (button1 == v)
{ try {
URL url = new URL("http://xxxxx.de/txt/test.txt");
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(false);
con.setReadTimeout(20000);
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101
Firefox/16.0");
((HttpURLConnection) con).setRequestMethod("GET");
//System.out.println(con.getContentLength()) ;
con.setConnectTimeout(5000);
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println(responseCode);
}
StringBuffer buffer = new StringBuffer();
int chars_read;
//int total = 0;
while ((chars_read = in.read()) != -1)
{
char g = (char) chars_read;
buffer.append(g);
}
final String page = buffer.toString();
TextView ausgabe = (TextView)findViewById(R.id.ausgabe);
ausgabe.setText(page);
}
catch (Exception exception)
{
//NULL
}
}
Variante 2
Code:
public void onClick(View v) {
if (button1 == v)
{
try {
// Create a URL for the desired page
URL url = new URL("xxxxx.de/txt/test.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String text;
while ((text = in.readLine()) != null)
{
TextView ausgabe = (TextView)findViewById(R.id.ausgabe);
ausgabe.setText(text);
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
Variante 3
Code:
public void onClick(View v) {
if (button1 == v)
{
HttpGet httppost = new HttpGet("http://xxxxx.de/txt/test.txt");
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
TextView ausgabe = (TextView)findViewById(R.id.ausgabe);
ausgabe.setText(total);
}
Das ganze kann doch nicht so schwer sein?!
Bitte helft mir, bin langsam echt frustriert -.-
mfg
Felix