IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

  1. #1
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2011
    Messages
    9
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activit� : Consultant informatique
    Secteur : High Tech - Produits et services t�l�com et Internet

    Informations forums :
    Inscription : Juillet 2011
    Messages : 9
    Par d�faut Comment lire le contenu d'une URL (http://www.mondomain.com) comme pour les fichiers ?
    Bonjour,

    Voici mon but:
    Lire le contenu d'une page web (le mien) et la transmettre dans une variable.
    Pour faire du "parcing" apr�s et l'affich� a ma convenance.

    Niveau de connaissance: Novice

    Voici mon environnement de travaille:
    IDE Eclipse version: Indigo Release
    SDK Android
    �mulateur Android API 2.2

    Mon 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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    package com.android.snmp;
     
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    import java.net.*;
     
    public class Snmp extends Activity
    {
    	private String infoURL(String args) throws IOException {
    		URL u = new URL(args);
            BufferedInputStream bis = new BufferedInputStream(u.openStream());
     
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("output.txt")));
    while(bis.available() != 0)
    {
            byte[] data = new byte[bis.available()];
            System.out.println("Data length : " + data.length);
            bis.read(data, 0, data.length);
            bos.write(data, 0, data.length);
    }
            bis.close();
            bos.close();
    		return args;
    	}
     
    	public void onCreate(Bundle icicle)
    	{
           super.onCreate(icicle);
           TextView textView = new TextView(this);
     
    		String info_url = "";
    		try {
    			info_url = infoURL("http://www.mondomain.com/");
    		} catch (IOException e) {
    			info_url = "IOException";
    		}
    	    textView.setText(info_url);
    	    setContentView(textView);
        }
     
    }
    ---------------------------------

    J'ai fais mes devoirs et test� sur divers version d'�mulateur de 1.6 a 3.1.
    test� divers code exemple fournie dans divers forum.
    J'ai toujours l'erreur suivant qui m'affiche dans l'�mulateur d'android lorsque j'ex�cute le .apk : IOException

    Dans Eclipe tout s'emble beau, dans l'�mulateur je peux me connecter sur le web via l'application int�gr�.

    Merci pour vos suggestions.

    -Ian

  2. #2
    Membre chevronn�
    Avatar de ZouBi
    Inscrit en
    Octobre 2007
    Messages
    508
    D�tails du profil
    Informations professionnelles :
    Secteur : High Tech - Multim�dia et Internet

    Informations forums :
    Inscription : Octobre 2007
    Messages : 508
    Par d�faut
    et � part l'IOException, qu'est qui te dit?

  3. #3
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2011
    Messages
    9
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activit� : Consultant informatique
    Secteur : High Tech - Produits et services t�l�com et Internet

    Informations forums :
    Inscription : Juillet 2011
    Messages : 9
    Par d�faut Mon stacktrace
    Merci pour votre aide

    -Ian

    -----
    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
    java.net.UnknownHostException: Unable to resolve host "www.google.com": Mo Adress associated whith hostname
     at java.net.InetAdress.lookupHostByName(InetAdress.java.496)
     at java.net.InetAdress.getAllByName(InetAdress:java 277)
     at java.net.InetAdress.getAllByname(InetAdress:java 249)
     at org.apache.harmony.luni.internal.net.wwww.protocol.httpConnection.<init>(HttpConnection.java:69)
     at org.apache.harmony.luni.internal.net.wwww.protocol.httpConnection.<init>(HttpConnection.java:48)
     at org.apache.harmony.luni.internal.net.wwww.protocol.httConnection$Adress.connect(HttpConnection.java:304)
     at org.apache.harmony.luni.internal.net.wwww.protocol.httConnectionPool.get(HttpConnectionPool.java:89)
     at org.apache.harmony.luni.internal.net.wwww.protocol,httpConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
     at org.apache.harmony.luni.internal.net.wwww.protocol,httpConnectionImpl.makeConnection(httpURLConnectionImpl.java:274)
     at org.apache.harmony.luni.internal.net.wwww.protocol,httpConnectionImpl.tretieveresponse(HttpURLConnectionImpl.java:1038)
     at org.apache.harmony.luni.internal.net.wwww.protocol,httpConnectionImpl.getInputStream(HttpURLConnectionImpl.java:522)
     at java.net.URL.openStream(URL.java:645)
     at com.android.snmp.Snmp.onCreate(Snmp.java:56)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
     at android.app.ActivityThread.handledLaunActivuty(ActivityThread.java:1764)
     at android.app.ActivityThread.access$1500(activityThread.java:122)
     at android.app.ActivityThread$H.handleMassege(ActivityThread.java:1002)
     at andeoid.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:132)
     at android.app.ActivityThread.main(ActivityThread.java:4025)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invokeNative(Metho.java:491)
     at com.android.Internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
     at com.android.Internal.os.ZygoteInit.main(ZygoteInit.java:599)
     at dalvik.system.NativeStart.main(Native Method)
    -----

  4. #4
    Expert confirm�

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par d�faut
    Bonjour,


    1/ As tu bien mis la permission INTERNET
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    android.permission.INTERNET
    2/ Arrives tu � te connecter via le browser par d�fault
    Si non alors tu as un probl�me sur ta connexion ( proxy , ...)

    3/ Essaye de recr�er un emulateur, d�s fois cela arrange le probl�me

  5. #5
    Membre averti
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2011
    Messages
    9
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activit� : Consultant informatique
    Secteur : High Tech - Produits et services t�l�com et Internet

    Informations forums :
    Inscription : Juillet 2011
    Messages : 9
    Par d�faut Resolu
    Merci pour l'info,
    c'�tait effectivement d'ajout�:

    <uses-permission android:name="android.permission.INTERNET" />

    juste avant /manifest dans: AndroidManifest.xml

    J'en ai profit� pour changer mon code, afficher tout le contenu de la page url. sans pass� par un ficher. je vous fais par de mon code.

    -Ian


    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.io.StringWriter;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    import java.net.*;
     
    public class Snmp extends Activity
    {
    	public static String stack2string(Exception e) {
    		  try {
    		    StringWriter sw = new StringWriter();
    		    PrintWriter pw = new PrintWriter(sw);
    		    e.printStackTrace(pw);
    		    return "------\r\n" + sw.toString() + "------\r\n";
    		  }
    		  catch(Exception e2) {
    		    return "bad stack2string";
    		  }
    		 }
     
    	private String infoURL(String url) throws IOException {
            URL tmpURL = new URL(url);
            URLConnection yc = tmpURL.openConnection();
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    yc.getInputStream()));
            String inputLine = "no data";
            String inputTMP = "";
           while ((inputTMP = in.readLine()) != null) {
        	   inputLine = inputLine + inputTMP;
           }
                 System.out.println(inputLine);
            in.close();
    		return inputLine;
    	}
     
    	public void onCreate(Bundle icicle)
    	{
           super.onCreate(icicle);
           TextView textView = new TextView(this);
     
    		String info_url = "";
    		try {
    			info_url = infoURL("http://www.monurl.com/");
    		} catch (IOException ioe) {
    			ioe.printStackTrace();
    			info_url = stack2string(ioe);
    		}
    	    textView.setText(info_url);
    	    setContentView(textView);
        }
     
    }

  6. #6
    Membre averti
    Inscrit en
    Mai 2011
    Messages
    23
    D�tails du profil
    Informations forums :
    Inscription : Mai 2011
    Messages : 23
    Par d�faut
    Si d'autres on le probl�me il faut comme dit mettre la permission en dernier juste avant </manifest>
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
     
    </application>
        <uses-permission android:name="android.permission.INTERNET" />
    </manifest>
    Dans mon cas j'avais mis avant </application> et �a ne passait pas.

  7. #7
    Expert confirm�

    Homme Profil pro
    Ing�nieur syst�mes et r�seaux
    Inscrit en
    F�vrier 2007
    Messages
    4 253
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations professionnelles :
    Activit� : Ing�nieur syst�mes et r�seaux
    Secteur : High Tech - Multim�dia et Internet

    Informations forums :
    Inscription : F�vrier 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par d�faut
    Il suffit d�diter le manifest avec eclipse... le plugin se charge de le mettre au bon endroit

+ R�pondre � la discussion
Cette discussion est r�solue.

Discussions similaires

  1. Probl�me pour lire le contenu d'une url
    Par eric41 dans le forum Langage
    R�ponses: 3
    Dernier message: 25/12/2012, 23h30
  2. lire le contenu d'une URL
    Par turin75 dans le forum Haskell
    R�ponses: 3
    Dernier message: 19/04/2008, 19h34
  3. Comment lire le contenu d'une cellule
    Par lthibert dans le forum G�n�ral JavaScript
    R�ponses: 1
    Dernier message: 24/01/2007, 17h21
  4. Comment r�cup�rer le contenu d'une URL dans une variable ?
    Par Fildz dans le forum G�n�ral JavaScript
    R�ponses: 2
    Dernier message: 28/08/2006, 09h26
  5. [D�butant]Comment lire le contenu d'une Listview ?
    Par PierreBord dans le forum Composants VCL
    R�ponses: 2
    Dernier message: 13/02/2006, 16h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo