使用带有Android额外标题的网址打开浏览器 [英] Open browser with a url with extra headers for Android

查看:141
本文介绍了使用带有Android额外标题的网址打开浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a specific requirement where I have to fire a url on the browser from my activity. I am able to do this with the following code :

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。说
(用户:用户名)
我该如何实现?
提前多多谢谢!

Now, I want to invoke this webseal_sso_endpoint by passing an extra header. say ("user":"username") How can I implement this? Thanks a lot in advance!

推荐答案

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

Recommended method to do this would be to use the Uri class to create your URI. Helps ensure that everything is defined correctly and associates the correct key to value for your URI.

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

For example, you want to send a Web intent with this URL:

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

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

And you have a defined URL and parameters you want to send, you should declare these as static final fields, like so:

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";

然后您可以使用这些,如下所示:

You can then use these, like so:

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

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

Now if you want to add another parameter, add another appendQueryParameter, like so:

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

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

You can convert to a URL if needed using:

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

应该是这样的:

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

这篇关于使用带有Android额外标题的网址打开浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆