使用带有Android额外标题的网址打开浏览器

时间:2015-03-05 02:03:37

标签: java android uri

我有一个特定的要求,我必须从我的活动中在浏览器上触发一个URL。我可以使用以下代码执行此操作:

Intent browserIntent = new Intent(
  Intent.ACTION_VIEW, Uri.parse(
    pref.getString("webseal_sso_endpoint", "") + "?authorization_code="
      + code + "&webseal-ip=" + websealIP
  )
);

activity.startActivity(browserIntent);
activity.finish();

现在,我想通过传递额外的标头来调用此webseal_sso_endpoint。说 ("用户":"用户名&#34) 我该如何实现呢? 非常感谢提前!

2 个答案:

答案 0 :(得分:20)

我介绍了如何添加标题。这是我的代码:

        Intent browserIntent = new Intent(
                Intent.ACTION_VIEW, Uri.parse(url));
        Bundle bundle = new Bundle(); 
        bundle.putString("iv-user", username); 
        browserIntent.putExtra(Browser.EXTRA_HEADERS, bundle);
        activity.startActivity(browserIntent);
        activity.finish();

答案 1 :(得分:1)

这样做的推荐方法是使用Uri类来创建URI。帮助确保正确定义所有内容并将正确的密钥与URI的值相关联。

例如,您希望使用以下URL发送Web意图:

http://webseal_sso_endpoint?authorization_code=SomeCode&webseal-ip=WEBSEALIP

你有一个你想要发送的定义的URL和参数,你应该将它们声明为静态最终字段,如下所示:

private final static String BASE_URL = "http://webseal_sso_endpoint";
private final static String AUTH_CODE = "authorization_code";
private final static String IP = "webseal-ip";  
private final static String USERNAME = "user";

然后您可以使用它们,如下所示:

Uri builtUri = Uri.parse(BASE_URL).buildUpon()
    .appendQueryParameter(AUTH_CODE, code)
    .appendQueryParameter(IP, websealIP)
    .build();

现在,如果您想添加另一个参数,请添加另一个appendQueryParameter,如下所示:

Uri builtUri = Uri.parse(BASE_URL).buildUpon()
    .appendQueryParameter(AUTH_CODE, code)
    .appendQueryParameter(IP, websealIP)
    .appendQueryParameter(USERNAME, user)
    .build();

如果需要,您可以使用以下方式转换为网址:

URL url = new URL(builtUri.toString());

应该是这样的:

http://webseal_sso_endpoint?authorization_code=SomeCode&webseal-ip=WEBSEALIP&user=SomeUsersName