Bonjour à tou-te-s,
Je souhaite réaliser une activity dans laquelle il y ait une image et un textview contenant des données récupérées texte sur le web.
Pour faire cela j'ai crée un fichier txt que j'ai mis en ligne sur un serveur web et j'appelle et lis le texte de ce fichier dans un textview.
Mon soucis est que je n'arrive à afficher _que_ le texte du fichier txt.
Voici le code de l'activity
Le texte s'affiche mais ne prend pas en compte mon fichier main.xml dont voici le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* We will show the data we read in a TextView. */ TextView tv = new TextView(this); /* Will be filled and displayed later. */ String myString = null; try { /* Define the URL we want to load data from. */ URL myURL = new URL("http://urldusite/patati.txt"); /* Open a connection to that URL. */ URLConnection ucon = myURL.openConnection(); /* Define InputStreams to read * from the URLConnection. */ InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); /* Read bytes to the Buffer until * there is nothing more to read(-1). */ ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while((current = bis.read()) != -1){ baf.append((byte)current); } /* Convert the Bytes read to a String. */ myString = new String(baf.toByteArray()); } catch (Exception e) { /* On any Error we want to display it. */ myString = e.getMessage(); } /* Show the String on the GUI. */ tv.setText(myString); this.setContentView(tv); }
J'ai testé pas mal de choses en vain. Merci d'avance pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#EAEAEA"> <ImageView android:src="@drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Partager