2014-09-22 59 views

回答

1

使用此片段:

XWalkView mXWalkView; 
mXWalkView.getSettings().setUserAgentString("Your User Agent"); 
+1

请添加一些关于您的代码在做什么以及如何工作的解释,以便像OP这样的新手能够从回答。 – 2014-10-04 10:48:41

+0

没有解决我的实际问题,但服务器识别新的用户代理。 谢谢! :) – Jonson 2014-10-06 16:00:51

+1

从此人行横道9将不再工作。 – Ostkontentitan 2014-12-03 09:57:31

3

在文档是没有什么比它。显然,唯一的方法是使用setResourceClient和WebResourceResponse更改用户代理。 使用示例:

setResourceClient(new XWalkResourceClient(this) { 
    @Override 
    public WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) { 
     try { 
      URL u = new URL(url); 
      HttpURLConnection c = (HttpURLConnection) u.openConnection(Proxy.NO_PROXY); 
      c.setRequestProperty("User-agent", "test user agent"); 
      return new WebResourceResponse(null, "utf-8", c.getInputStream()); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return super.shouldInterceptLoadRequest(view, url); 
    } 
}); 
+0

它可以工作,但会降低性能和加载时间。这是一个非常有趣的答案,我学到了很多东西。 谢谢! – Jonson 2014-10-06 16:02:59

+0

该解决方案可能仅用于第一个url(用于加载其他资源,图片等的skil) – Kenumir 2014-10-07 07:12:32

0

这是Web的应用程序,你可以改变JS-文件:

我使用此解决方案,它的工作原理以及人行横道9.

xview.addJavascriptInterface(new Object(){ 
      @JavascriptInterface 
      public String getNewUserAgent(){ 
       return "My User Agent"; 
      } 
     }, "NativeInterface"); 

而在我的网络应用程序我ju st电话:

navigator.ua = NativeInterface.getNewUserAgent(); 
+0

谢谢。到现在为止,我使用Crosswalk 8,但这是一个有趣的信息! – Jonson 2014-12-04 11:41:35

1

设置用户代理的新API正在草拟中,并将在明年1月底的Crosswalk-12中发布。

它会像: xwalkView.setUserAgentString(newUserAgentString);

+0

来源:https://www.mail-archive.com/[email protected]/msg00634.html – cprcrack 2014-12-17 19:10:14

5

我用反射来解决这个问题,直到这个API在Crosswalk 12中再次公开。这适用于9.38.208.10的Crosswalk。

private void setWebViewUserAgent(XWalkView webView, String userAgent) 
{ 
    try 
    { 
     Method ___getBridge = XWalkView.class.getDeclaredMethod("getBridge"); 
     ___getBridge.setAccessible(true); 
     XWalkViewBridge xWalkViewBridge = null; 
     xWalkViewBridge = (XWalkViewBridge)___getBridge.invoke(webView); 
     XWalkSettings xWalkSettings = xWalkViewBridge.getSettings(); 
     xWalkSettings.setUserAgentString(userAgent); 
    } 
    catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) 
    { 
     // Could not set user agent 
     e.printStackTrace(); 
    } 
} 
+0

感谢它的工作。 – Ataru 2014-12-25 11:45:54

+0

嘿,看起来'getSettings'不再可用。有没有其他的方法来做到这一点? – tehmaestro 2015-09-27 12:57:24

相关问题