开发者

Problems loading mobile.twitter in webview

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 th开发者_开发百科is worked a month or so ago. Anyone else experiencing this?

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


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 :)


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.


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.


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) {}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜