使用Android中的URI制造商或创建变量网址 [英] Use URI builder in Android or create URL with variables
问题描述
我开发一个Android应用程序。我需要建立一个URI我的应用程序,使一个API请求。除非有另一种方式把一个变量的URI,这是我发现的最简单的方法。我发现,你需要使用 Uri.Builder
,但我不太清楚如何。我的网址是:
I'm developing an Android app. I need to build a URI for my app, to make an API request. Unless there's another way to put a variable in a URI, this is the easiest way I've found. I found that you need to use Uri.Builder
, but I'm not quite sure how to. My url is:
http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=[redacted]&mapid=`value`.
我的方案是HTTP,权威 lapi.transitchicago.com
,路径 /api/1.0
,路径段 ttarrivals.aspx
,和查询 *键= [删除]放大器;的azazaz =
值 *
。
My scheme is http, authority lapi.transitchicago.com
, path /api/1.0
, path segments ttarrivals.aspx
, and query *key=[redacted]&mapid=
value*
.
我的code是如下:
Intent intent = getIntent();
String value = intent.getExtras().getString("value");
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority("www.lapi.transitchicago.com")
.appendPath("api")
.appendPath("1.0")
.appendPath("ttarrivals.aspx")
.appendQueryParameter("key", "[redacted]")
.appendQueryParameter("mapid", value);
我明白,我做uri.add,但我要如何将它集成到 Uri.Builder
?我会添加的每一件事情,就像 URI.add(计划)
, URI.add(权威)
,等?或者是不是要做到这一点?此外,有没有给一个变量添加到URI / URL,它是比较容易的任何其他方式?
I understand that I do uri.add, but how do I integrate it into the Uri.Builder
? Would I add every thing, like URI.add(scheme)
, URI.add(authority)
, and so on? Or is that not the way to do it? Also, is there any other way to add a variable to a URI/URL that is easier?
感谢您的帮助。
推荐答案
让我们说,我想创建以下网址:
Let's say that I want to create the following URL:
https://www.myawesomesite.com/turtles/types?type=1&sort=relevance#section-name
要建立这个与<一个href="http://developer.android.com/reference/android/net/Uri.Builder.html"><$c$c>Uri.Builder$c$c>我会做到以下几点。
To build this with the Uri.Builder
I would do the following.
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("www.myawesomesite.com")
.appendPath("turtles")
.appendPath("types")
.appendQueryParameter("type", "1")
.appendQueryParameter("sort", "relevance")
.fragment("section-name");
String myUrl = builder.build().toString();
这篇关于使用Android中的URI制造商或创建变量网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!