11

When I try to load the following urls in a Webview all i get is black twitter background with loading spinner. The page is loaded, as WebViewClient.onPageFinished is called. However the page loads ok in the standard Android browser.

https://twitter.com/#!/scottyab or https://mobile.twitter.com/#!/scottyab

I'm thinking Twitter changed their mobile website as this worked a month or so ago. Anyone else experiencing this?

Updated: javascript enabled mWebView.getSettings().setJavaScriptEnabled(true);

scottyab
  • 23,621
  • 16
  • 94
  • 105

4 Answers4

19

Those are indeed dirty hacks. Changing the User Agent is really a nasty solution and should never be done. When loading twitter.com in a webview, you'd better try the piece of code given there : https://stackoverflow.com/a/6625418/162178

For lazy clickers I'll give it here :

webView.getSettings().setDomStorageEnabled(true);

All credits goes to gregm, who gave this one :)

Happy coding !

Edit: Just a little update to justify this choice, User Agent are meant to give the visited site the info about who's the client. If one day Twitter makes special changes dedicated to Android, they will definitely use the User Agent to achieve that. If you tell them your an iPhone or whatever you might never get redirected or more simply never get the css intended specially for Android.

And in a more political matter, if everyone changes it's User Agent, site statistics will be wrong and they might never see there are a lot of Androids coming to their website. ^^ (To maybe consider engaging the proper updates to their website). And this whole thing is not only for Twitter web clients. So be nice.

Benjamin's answers here seems quite good to (using Java's Reflection to make it backwards compatible if I'm correct)

So again don't change the User Agent it is very bad for your app and the web health. And should simply be banned from any code out there. Thanks :)

Community
  • 1
  • 1
MrBuBBLs
  • 459
  • 6
  • 15
  • Worked perfectly! Thanks for the link =) – Alesqui Jan 09 '12 at 19:24
  • I am facing an issue here. When i do webView.getSettings().setDomStorageEnabled(true); and then call loadURL for twitter for the first time, page is not loaded . But if i call webView.reload() or load the url again , it opens twitter.com from then onwards, until i exit the app (or force close from settings) . Again from next restart, twitter is not loaded for 1st time. Has anyone faced this issue ? – Alok Kulkarni Aug 09 '12 at 14:12
  • @AlokKulkarni I see the first-time page seeming to load, but showing no tweats, only showing 4 list items (Following, Followers, Favorites, Lists) that do not respond to a click. Refreshing the page shows the expected list of tweets, and below the tweets those list work. I assume mobile.twitter.com is broken for the first view, but it succeeds at setting some html5 flag via the DOM storage. – larham1 Oct 22 '12 at 16:42
  • @AlokKulkarni using the iphone ua agent works well on the first view, without the dom storage setting. I'm adding a conditional for twitter.com. Hope twitter supports android better in the future. – larham1 Oct 22 '12 at 16:45
  • 1
    Ok thanks !Setting the user agent along with DomStorageenabled did the trick for me – Alok Kulkarni Oct 23 '12 at 06:51
  • I tired this solution enabled the webView.getSettings().setDomStorageEnabled(true); also user agent but not working for me still am getting web page not available.while opening the twitter user please suggest a solution. – RAHULRSANNIDHI Oct 20 '15 at 05:53
16

Fixed the loading issue by hardcoding the user agent to iPhone's user agent (I found it worked on the iPhone version of the app)

mWebView.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");

Need to wash my hands after this a dirty hack.

scottyab
  • 23,621
  • 16
  • 94
  • 105
14

Slightly less dirty than using the iPhone's user agent, I was able to get this to work using the OG Droid's user agent string:

webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");

This way you don't get iPhone specific text like "Download the new Twitter App for iPhone" on the page.

chuckbjones
  • 374
  • 2
  • 7
2

Following MrBUBBLs answer, something like that should do it to manage backward compatibility while solving twitter issue:

// Hack to let mobile twitter work. From API 7 (Android 2.1) only
try {
    Method m = WebSettings.class.getMethod("setDomStorageEnabled", new Class[]{boolean.class});
    m.invoke(mWebView.getSettings(), true);
} 
catch (SecurityException e) {} 
catch (NoSuchMethodException e) {} 
catch (IllegalArgumentException e) {} 
catch (IllegalAccessException e) {} 
catch (InvocationTargetException e) {}
Benjamin Piette
  • 3,645
  • 1
  • 27
  • 24