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

Android Discussion :

récupération du code HTML à partir d'URL


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Femme Profil pro
    Etudiante
    Inscrit en
    Juillet 2010
    Messages
    164
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Etudiante

    Informations forums :
    Inscription : Juillet 2010
    Messages : 164
    Par défaut récupération du code HTML à partir d'URL
    Bonjour,
    Je veux récupérer la page Html à partir de l'URL j'ai trouvé un code que j'ai l'essayé mais un erreur se produit lors de l’exécution.
    Voici le code que j'ai utilisé . Pouvez vous m'aider

    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
    package com.load.page_html;
     
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
     
    public class MyJavaScriptInterface extends Activity {
        /** Called when the activity is first created. */
    	WebSettings websettings;
     
        final Context myApp = this;
     
        /* An instance of this class will be registered as a JavaScript interface */
        @Override
        public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main);
        final WebView browser = (WebView)findViewById(R.id.browser);   
     
        /* JavaScript must be enabled if you want it to work, obviously */
        websettings = browser.getSettings();
        websettings.setJavaScriptEnabled(true);
     
        /* Register a new JavaScript interface called HTMLOUT */
        browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
     
        /* WebViewClient must be set BEFORE calling loadUrl! */
        browser.setWebViewClient(new WebViewClient() {
     
            public void onPageFinished(WebView view, String url)
            {
                /* This call inject JavaScript into the page which just finished loading. */
                browser.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
            }
        });
     
        /* load a web page */
        browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");
    }}

  2. #2
    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,

    Peux tu nous dire l'erreur que tu as ? Tu l'as trouvera dans le logcat .

    Merci.

  3. #3
    Membre confirmé
    Femme Profil pro
    Etudiante
    Inscrit en
    Juillet 2010
    Messages
    164
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Etudiante

    Informations forums :
    Inscription : Juillet 2010
    Messages : 164
    Par défaut
    Le message d'erreur d'exécution c'était
    L'application Clavier Android (du processus com.android.inputmethod.latin) ne répond pas

    et dans la fenêtre du Log Cat

    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
     
    03-18 10:36:48.563: ERROR/AndroidRuntime(207): Uncaught handler: thread main exiting due to uncaught exception
    03-18 10:36:48.624: ERROR/AndroidRuntime(207): android.app.SuperNotCalledException: Activity {com.load.page_html/com.load.page_html.MyJavaScriptInterface} did not call through to super.onCreate()
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2461)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at android.os.Looper.loop(Looper.java:123)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at android.app.ActivityThread.main(ActivityThread.java:4363)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at java.lang.reflect.Method.invoke(Method.java:521)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    03-18 10:36:48.624: ERROR/AndroidRuntime(207):     at dalvik.system.NativeStart.main(Native Method)

  4. #4
    Rédacteur
    Avatar de Viish
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2009
    Messages
    427
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2009
    Messages : 427
    Par défaut
    Bonjour,

    Si tu avais lu le message d'erreur renvoyé par le Logcat, tu aurais vu que ton Acitivity n'appelles pas super.onCreate() dans la méthode onCreate().

    Rajoute donc cette ligne au début de ta méthode et ça devrait fonctionner.

    A part cela je pense qu'il y a mieux pour récupérer le code d'une page HTML :
    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
     
                    HttpClient httpClient = new DefaultHttpClient();
    		HttpGet httpGet = new HttpGet();
    		URI uri = new URI("http://www.thinkdroid.eu");
    		httpGet.setURI(uri);
    		HttpResponse httpResponse = httpClient.execute(httpGet);
    		InputStream inputStream = httpResponse.getEntity().getContent();
    		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
     
    		String ligneCodeHTML = bufferedReader.readLine();
    		StringBuffer stringBuffer = new StringBuffer("");
    		while (ligneCodeHTML != null)
    		{
                            stringBuffer.append(ligneCodeHTML + "\n");
    			ligneCodeHTML = bufferedReader.readLine();
    		}
    Puis pour l'afficher
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
                    WebView webView = new WebView(this);
    		webView.loadDataWithBaseURL("fake:", stringBuffer.toString(), "text/html", "utf-8", "");

  5. #5
    Membre confirmé
    Femme Profil pro
    Etudiante
    Inscrit en
    Juillet 2010
    Messages
    164
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Etudiante

    Informations forums :
    Inscription : Juillet 2010
    Messages : 164
    Par défaut
    J'ai utilisé votre 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
    package com.load.page_html;
     
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URI;
     
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebView;
     
    public class Main extends Activity {
    	public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            try{
            HttpClient httpClient = new DefaultHttpClient();
    		HttpGet httpGet = new HttpGet();
    		URI uri = new URI("http://www.thinkdroid.eu");
    		httpGet.setURI(uri);
    		HttpResponse httpResponse = httpClient.execute(httpGet);
    		InputStream inputStream = httpResponse.getEntity().getContent();
    		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
     
    		String ligneCodeHTML = bufferedReader.readLine();
    		StringBuffer stringBuffer = new StringBuffer("");
    		while (ligneCodeHTML != null)
    		{
                            stringBuffer.append(ligneCodeHTML + "\n");
    			ligneCodeHTML = bufferedReader.readLine();
    		}
    		WebView webView = new WebView(this);
    		webView.loadDataWithBaseURL("fake:", ligneCodeHTML, "text/html", "utf-8", "");
    }
            catch (Exception e) {
                e.printStackTrace();
            }
            }
    }
    et dans le LogCat j'obtiens
    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
    03-18 12:58:46.118: ERROR/AndroidRuntime(201): Uncaught handler: thread main exiting due to uncaught exception
    03-18 12:58:46.258: ERROR/AndroidRuntime(201): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.load.page_html/com.load.page_html.MyJavaScriptInterface}: java.lang.ClassNotFoundException: com.load.page_html.MyJavaScriptInterface in loader dalvik.system.PathClassLoader@43b86860
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.os.Looper.loop(Looper.java:123)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.app.ActivityThread.main(ActivityThread.java:4363)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at java.lang.reflect.Method.invoke(Method.java:521)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at dalvik.system.NativeStart.main(Native Method)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201): Caused by: java.lang.ClassNotFoundException: com.load.page_html.MyJavaScriptInterface in loader dalvik.system.PathClassLoader@43b86860
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
    03-18 12:58:46.258: ERROR/AndroidRuntime(201):     ... 11 more

  6. #6
    Membre confirmé
    Femme Profil pro
    Etudiante
    Inscrit en
    Juillet 2010
    Messages
    164
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Etudiante

    Informations forums :
    Inscription : Juillet 2010
    Messages : 164
    Par défaut
    Pour le premier code, l'erreur est corrigé mais je n'ai pas pu récupérer le code HTML.
    En fait rien ne s'affiche.

Discussions similaires

  1. récupération du code HTML dans une zone de texte
    Par deubelte dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 26/01/2011, 23h25
  2. Réponses: 16
    Dernier message: 15/06/2010, 15h40
  3. Récupération du code html d'un page web.
    Par hassine dans le forum C#
    Réponses: 7
    Dernier message: 11/08/2009, 10h49
  4. Code Html pour lien URL
    Par nettoyerforum dans le forum Balisage (X)HTML et validation W3C
    Réponses: 7
    Dernier message: 15/06/2007, 09h42
  5. créer du code html à partir d'objets
    Par maa dans le forum ASP.NET
    Réponses: 4
    Dernier message: 28/05/2007, 01h24

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