Gabin Posted February 10, 2011 Share Posted February 10, 2011 Bonsoir, Toujours dans ma programmation de l'application MonPlanning pour l'Université d'Orléans (ok pas très original comme nom ^^), je me retrouve face à un problème. Je n'arrive pas à forcer le chargement de ma webview à tous les coups ! Je vais essayer d'imager le plus possible mon propos. Tout d'abord voici mon fichier : WebPlanning.java package net.mecadev.empdtps.univorleans; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; public class WebPlanning extends Activity{ WebView planningView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webplanning); String urlPlanning = Main.getUrlPlanningURI(); // Affichage de l'addresse /*TextView tv = (TextView)findViewById(R.id.text1); tv.setText(urlPlanning);*/ planningView = (WebView) findViewById(R.id.wb_planning); //planningView.setWebViewClient(new Callback()); planningView.loadUrl(urlPlanning); /* Je charge l'adresse voulue */ planningView.loadUrl("http://www.univ-orleans.fr..." + variables ); /* Je charge une nouvelle page qui prend l'adresse précédente et affiche que l'image */ } /*public class Callback extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView view, String url) { planningView.loadUrl(Main.getUrlPlanningURI()); return true; } }*/ } Je fais ma sélection : Et il m'affiche la dernière adresse utilisée : Il faudra peut être plusieurs allez - retour sur le bouton Voir pour que je réussisse à afficher la page souhaitée. Comment puis je forcer le chargement de ma WebView à chaque fois que WebPlanning.java est appelé ? Comme vous pouvez le voir, j'ai essayé quelque chose comme setWebViewClient ainsi que shouldOverrideUrlLoading ... sans rien de très concluant ... Je vous remercie, Bonne soirée, Gabin Link to comment Share on other sites More sharing options...
Pierre87 Posted February 10, 2011 Share Posted February 10, 2011 pourquoi appelles tu 2 fois de suite "loadUrl()"? Link to comment Share on other sites More sharing options...
Gabin Posted February 11, 2011 Author Share Posted February 11, 2011 Parce que j'ai dans la première, un emploi du temps dynamique avec quelques liens en dessous de celui ci et que je veux obtenir qu'une image. Du coup, je charge à la suite l'Url pour normalement imprimer la page ce qui transforme l'emploi du temps dynamique en image. Voilà ! Link to comment Share on other sites More sharing options...
chpil Posted February 11, 2011 Share Posted February 11, 2011 Tu peux essayer de voir si vider le cache du webView, avant l'affichage de ta page, peut avoir un effet webView.clearCache(true); webView.loadUrl(monUrl); D'autre part, il y a mieux comme solution pour passer l'url à afficher de ton Activity appelante à l'Activity appelée que d'utiliser une méthode statique sur l'Activity appelante. Utilise plutôt les méthodes putExtra/getStringExtra sur Intent pour ce faire Dans l'Activity appelante: Intent intent = new Intent(...); intent.putExtra("monUrl", monUrl); startActivity(intent); Dans l'Activity appelée: public void onCreate(...) { // ... Intent intent = getIntent(); String monUrl = intent.getStringExtra("monUrl"); Link to comment Share on other sites More sharing options...
Gabin Posted February 11, 2011 Author Share Posted February 11, 2011 Chpil, tu réponds toujours présent ! Je vais finir par devoir ajouter ton pseudo dans mon application :P Je regarde ça dès ce soir et je te tient au courant ! Link to comment Share on other sites More sharing options...
Gabin Posted February 11, 2011 Author Share Posted February 11, 2011 Je sais pas si c'est vraiment le cache qui m****... Je pense que le fait de charger les 2 pages d'affilé pose problème. Comment pourrais je faire pour charger la première page puis une fois chargée, charger la seconde ? Je précise que je veux charger la 2ème par dessus la 1ère, donc ds la même webview afin d'effectuer une requête sur la 1ère avec la 2ème adresse. Oulaaa un peu dur l'explication... J'espère que quelqu'un pourra m'aider !? Pistes: Pouvoir vérifier le chargement d'une webview ? Pourquoi pas mettre un temps d'attente entre les 2 chargements ? (La 2ème pistes ne sera pas très "propre" par contre...) Merci à vous ! Link to comment Share on other sites More sharing options...
Pierre87 Posted February 11, 2011 Share Posted February 11, 2011 Tu peux savoir avec le WebViewClient si la page a fini de se charger Link to comment Share on other sites More sharing options...
Gabin Posted February 11, 2011 Author Share Posted February 11, 2011 Alors, j'ai essayé de mettre en place ce que tu m'as dis : package net.mecadev.empdtps.univorleans; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; public class WebPlanning extends Activity{ WebView planningView; String urlPlanning; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webplanning); Intent PlanningIntent = getIntent(); urlPlanning = PlanningIntent.getStringExtra("urlplanning"); String ID_sizeWidth = PlanningIntent.getStringExtra("ID_sizeWidth"); String ID_sizeHeight = PlanningIntent.getStringExtra("ID_sizeHeight"); TextView tv = (TextView)findViewById(R.id.text1); tv.setText(urlPlanning); this.planningView = (WebView) findViewById(R.id.wb_planning); WebSettings settings = planningView.getSettings(); settings.setJavaScriptEnabled(true); planningView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); planningView.clearCache(true); planningView.loadUrl(urlPlanning); planningView.loadUrl("http://www.univ-orleans.fr/ade/custom/modules/plannings/print.jsp?width=" + ID_sizeWidth +"&height=" + ID_sizeHeight + "&print=yes"); } } Mon problème est que mon url(ma page) urlPlanning n'a pas le temps de se charger et du coups l'autre page se charge. Le Hic c'est que si la première n'est pas chargée, la deuxième renvoie sur une page d'erreur. Avec le code que tu m'as suggéré, l'erreur est mieux gérée puisqu'elle reste dans ma webview alors qu'avant elle m'ouvrait une page internet (via le browser android). Comment pourrais je adaptée ce code pour forcer ma première page (urlPlanning) à se charger avant que la deuxième ne le fasse à son tour ? Encore merci pour l'intérêt que vous portez à mes questions ! Link to comment Share on other sites More sharing options...
Gabin Posted February 12, 2011 Author Share Posted February 12, 2011 Up ! Je suis toujours dans une impasse... Link to comment Share on other sites More sharing options...
Pierre87 Posted February 13, 2011 Share Posted February 13, 2011 http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished(android.webkit.WebView, java.lang.String) RTFM :) Link to comment Share on other sites More sharing options...
Gabin Posted February 13, 2011 Author Share Posted February 13, 2011 Oh My God ! Comment ai-je pu louper ça ! Je vais voir ça et je te remercierais une seconde fois quand ça marchera :emo_im_angel: Link to comment Share on other sites More sharing options...
Gabin Posted February 13, 2011 Author Share Posted February 13, 2011 Alors... J'ai essayé de mettre en œuvre le code trouvé dans ton lien mais je ne comprend pas comment faire exactement... Voici le code actuel : public class WebPlanning extends Activity{ WebView planningView; String urlPlanning; Button btnRetour; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webplanning); Intent PlanningIntent = getIntent(); btnRetour = (Button)findViewById(R.id.btn_retour); urlPlanning = PlanningIntent.getStringExtra("urlplanning"); String ID_sizeWidth = PlanningIntent.getStringExtra("ID_sizeWidth"); String ID_sizeHeight = PlanningIntent.getStringExtra("ID_sizeHeight"); /*TextView tv = (TextView)findViewById(R.id.text1); tv.setText(urlPlanning);*/ this.planningView = (WebView) findViewById(R.id.wb_planning); WebSettings settings = planningView.getSettings(); settings.setJavaScriptEnabled(true); planningView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } public void onPageFinished (WebView view, String url) { super.onPageFinished(planningView, url); } }); planningView.clearCache(true); planningView.loadUrl(urlPlanning); planningView.loadUrl("http://www.univ-orleans.fr/ade/custom/modules/plannings/print.jsp?width=" + ID_sizeWidth +"&height=" + ID_sizeHeight + "&print=yes"); btnRetour.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } Merci pour votre aide ! Link to comment Share on other sites More sharing options...
Pierre87 Posted February 13, 2011 Share Posted February 13, 2011 il faudrait peut être que tu l'utilises ton onPageFinished :/ et pas besoin d'utiliser shouldOverrideUrlLoading dans ton cas Link to comment Share on other sites More sharing options...
Gabin Posted February 13, 2011 Author Share Posted February 13, 2011 Le problème c'est justement ça... :P Étant débutant, je ne vois pas très bien comment l'appeler :| Pourrais tu m'expliquer brièvement ? Merci à toi pour ta réactivité ! Link to comment Share on other sites More sharing options...
Pierre87 Posted February 13, 2011 Share Posted February 13, 2011 "Notify the host application that a page has finished loading. This method is called only for main frame." Tu n'as pas à l’appeler, car elle est appelée lorsque la page a fini de se charger. Link to comment Share on other sites More sharing options...
Gabin Posted February 13, 2011 Author Share Posted February 13, 2011 Si j'ai bien compris, j'ai juste à mettre l'action recherchée après la chargement de la page, dans ma méthode onPageFinished ? Je me retrouverais donc avec : public class WebPlanning extends Activity{ WebView planningView; String urlPlanning; Button btnRetour; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webplanning); Intent PlanningIntent = getIntent(); btnRetour = (Button)findViewById(R.id.btn_retour); urlPlanning = PlanningIntent.getStringExtra("urlplanning"); final String ID_sizeWidth = PlanningIntent.getStringExtra("ID_sizeWidth"); final String ID_sizeHeight = PlanningIntent.getStringExtra("ID_sizeHeight"); TextView tv = (TextView)findViewById(R.id.text1); //tv.setText(urlPlanning); this.planningView = (WebView) findViewById(R.id.wb_planning); WebSettings settings = planningView.getSettings(); settings.setJavaScriptEnabled(true); planningView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } public void onPageFinished (WebView view, String url) { super.onPageFinished(planningView, url); planningView.loadUrl("http://www.univ-orleans.fr/ade/custom/modules/plannings/print.jsp?width=" + ID_sizeWidth +"&height=" + ID_sizeHeight + "&print=yes"); } }); planningView.clearCache(true); planningView.loadUrl(urlPlanning); //planningView.loadUrl("http://www.univ-orleans.fr/ade/custom/modules/plannings/print.jsp?width=" + ID_sizeWidth +"&height=" + ID_sizeHeight + "&print=yes"); btnRetour.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } Le problème est que je souhaite charger une url lorsque la première a finie. Si je met mon code comme ça, le code va attendre que la première url soit chargé pour chargé la deuxième url qui va à son tour une fois chargée être rechargée par elle même. Comment faire ? Encore merci Link to comment Share on other sites More sharing options...
Gabin Posted February 13, 2011 Author Share Posted February 13, 2011 Ok je clos ce post ! Merci à vous tous ! Encore une fois vous m'aurez bien aidé et surtout bien fait progressé =) Link to comment Share on other sites More sharing options...
chpil Posted February 14, 2011 Share Posted February 14, 2011 Pour éviter de boucler, dans onPageFinished, il suffit que tu testes l'url qui vient de se finir de charger public void onPageFinished (WebView view, String url) { if (url.equals(urlPlanning)) { view.loadUrl("http://www.univ-orleans.fr/ade/custom/modules/plannings/print.jsp?width=" + ID_sizeWidth +"&height=" + ID_sizeHeight + "&print=yes"); } } Link to comment Share on other sites More sharing options...
Gabin Posted February 14, 2011 Author Share Posted February 14, 2011 Toutes mes excuses chpil, je n'avais même pas mis la solution finale... Voilà mon code (qui correspond à la solution que tu me donnes) : public void onPageFinished (WebView view, String url) { super.onPageFinished(planningView, url); urlPrintPlanning = "http://www.univ-orleans.fr/ade/custom/modules/plannings/print.jsp?width=" + ID_sizeWidth +"&height=" + ID_sizeHeight + "&print=yes"; if(planningView.getOriginalUrl().equals(urlPrintPlanning)== false) { planningView.loadUrl(urlPrintPlanning); } } Encore merci. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.