使用URI构建器构建URL

时间:2016-07-13 20:34:58

标签: java android url uribuilder

编码网址 -

https://spreadsheets.google.com/tq?tqx=out:JSON&tq=select%20*%20where%20((A%20%3D%20201))&key=1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU

解码的网址 -

https://spreadsheets.google.com/tq?tqx=out:JSON&tq=select * where ((A= 201))&key=1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU

在URI构建器中,我们应该使用哪个URL进行编码或解码?

此外,给定代码无效: -

String xyz="1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU";
    String X="201";


    final String STUDENT_RECORD_URL="https://spreadsheets.google.com/tq?tqx=out:JSON&tq=select*where";
    final String Frag1 = "A";
    final String Frag2 =  "key";
    Builder builder = Uri.parse(STUDENT_RECORD_URL).buildUpon();
    builder.appendQueryParameter(Frag1,X);
    builder.appendQueryParameter(Frag2,xyz);
    String finalUri=builder.build().toString();

1 个答案:

答案 0 :(得分:1)

我认为你误解了你的查询参数是什么。它们应该是您网址中的键值对。对于您的网址https://spreadsheets.google.com/tq?tqx=out:JSON&tq=select * where ((A= 201))&key=1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU,构建它看起来像:

final String STUDENT_RECORD_URL="https://spreadsheets.google.com/tq";

Builder builder = Uri.parse(STUDENT_RECORD_URL).buildUpon();
builder.appendQueryParameter("txq", "out:JSON");
builder.appendQueryParameter("tq", "select * where ((A= 201))");
builder.appendQueryParameter("key", "1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU");

String finalUri = builder.build().toString();

appendQueryParameter方法会自动对您的参数进行网址编码。