如何在Html webview url中传递值?

时间:2016-03-03 08:11:30

标签: android webview

我想从原始文件夹加载webview中的html url,它正常工作

url = "file:///android_res/raw/a1.html";


        webView.loadUrl(url); 

但是我希望像这样在URL中传递一个值

String s = "1";
url = "file:///android_res/raw/a"+s+".html";

但它不起作用请帮助我如何实现这一点。

2 个答案:

答案 0 :(得分:0)

这与androidJava SE的方式相同。

将完整的URL放入URLEncoder

try {
    String url = "http://www.example.com/?id=123&art=abc";
    String encodedurl = URLEncoder.encode(url,"UTF-8");

    Log.d("TEST", encodedurl);
    } 
 catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} 

答案 1 :(得分:0)

首先,你无法保证System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 23.52.183.215:80 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.GetResponse() at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) at System.Xml.XmlTextReaderImpl.FinishInitUriString() at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context, XmlResolver uriResolver) at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext) at Microsoft.TeamFoundation.Server.WebAccess.Controllers.ApiCommonController.GetNews(Int32 maxCount) 是一个有用的文件路径。所以,这个方法不能像你计划的那样工作。

你可以用    webview.loadUrl( “JavaScript的:XXXX”);  将参数传递给html。

或使用url = "file:///android_res/raw/a"+s+".html";