URL with parameter in WebView not working in Android?

前端 未结 8 1469
南笙
南笙 2020-12-20 14:40

I am trying to call the loadUrl method in a webview with the below url

http://stage.realtylog.net/iPhone/functions.php?username=xxx&ID=xxx&act=readFileAndPri

8条回答
  •  情书的邮戳
    2020-12-20 15:18

    Yoy can use a List of NameValuePair and URLEncodedUtils to create the url string..

    protected String addLocationToUrl(String url){
        if(!url.endsWith("?"))
            url += "?";
    
        List params = new LinkedList();
    
        if (lat != 0.0 && lon != 0.0){
            params.add(new BasicNameValuePair("lat", String.valueOf(lat)));
            params.add(new BasicNameValuePair("lon", String.valueOf(lon)));
        }
    
        if (address != null && address.getPostalCode() != null)
            params.add(new BasicNameValuePair("postalCode", address.getPostalCode()));
        if (address != null && address.getCountryCode() != null)
            params.add(new BasicNameValuePair("country",address.getCountryCode()));
    
        params.add(new BasicNameValuePair("user", agent.uniqueId));
    
        String paramString = URLEncodedUtils.format(params, "utf-8");
    
        url += paramString;
        return url;
    }
    

    Alternate option is like following...you can try this also...

    HttpPost postMethod = new HttpPost("your url");
    List nameValuePairs = new ArrayList();
    
    nameValuePairs.add(new BasicNameValuePair("your parameter","parameter value"));
    nameValuePairs.add(new BasicNameValuePair("your parameter","parameter value"));
    
    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    DefaultHttpClient hc = new DefaultHttpClient();
    
    HttpResponse response = hc.execute(postMethod);
    

提交回复
热议问题