建立网址-两行混淆 [英] Building URL- Confusion on a couple of lines

查看:113
本文介绍了建立网址-两行混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解Udacity Android开发课程中涉及的此类/方法,我在几行中感到困惑.我想知道String Param_QUERY ="q"的工作原理,并寻找任何解释.另外,我也对PARAM_SORT和sortBy感到困惑.对于这三个变量以及如何使用它们的任何解释将不胜感激.谢谢,对于任何奇怪的格式,我们深表歉意.

I am trying to understand this class/method involved in a Udacity Android Development course, and I am confused on a couple lines. I was wondering how the String Param_QUERY = "q" works, and looking for any explanation. In addition, I was also confused about PARAM_SORT, and sortBy. Any explanation on these three variables and how they are used would be much appreciated. Thanks, and sorry for any strange formatting.

public class NetworkUtils {

     final static String GITHUB_BASE_URL =
             "https://api.github.com/search/repositories";

     final static String PARAM_QUERY = "q";

     final static String PARAM_SORT = "sort";
     final static String sortBy = "stars";

/**
 * Builds the URL used to query Github.
 *
 * @param githubSearchQuery The keyword that will be queried for.
 * @return The URL to use to query the weather server.
 */
public static URL buildUrl(String githubSearchQuery) {

    Uri builtUri = Uri.parse(GITHUB_BASE_URL).buildUpon()
            .appendQueryParameter(PARAM_QUERY, githubSearchQuery)
            .appendQueryParameter(PARAM_SORT, sortBy)
            .build();

    URL url = null;
    try {
        url = new URL(builtUri.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    return url;
}

推荐答案

这实际上不是Android问题,但是我将解释发生了什么.首先,代码正在做的事情是建立一个像这样的字符串:

This isn't really an Android issue, but I'll explain what is going on. First, what the code is doing is building a string like:

https://api.github.com/search/repositories?q=githubSearchQuery&sort=stars

这是一个REST端点,当您在GitHub上查询该页面时,它会搜索存储库,并使用通过q=githubSearchQuery传递的搜索字符串,当然会替换为您的输入,并根据sort=stars对结果进行排序,排序通过星数.现在,Java部分的工作方式如下:

This is a REST endpoint, and when you query that page on GitHub, it searches repositories, with the search string passed through q=githubSearchQuery, replaced with of course your input, and sorts the results according to sort=stars, sorting by the number of stars. Now, the Java part works as follows:

appendQueryParameter(PARAM_SORT, sortBy)

对于简单的情况,对于常量字符串,它指定了sort=stars部分,并添加了所需的&与其他参数链接.

For the simple case, with constant strings, this specifies the sort=stars part, plus adds any required & to chain with other parameters.

appendQueryParameter(PARAM_QUERY, githubSearchQuery)

这与q=githubSearchQuery类似,但是由于这是用户输入的内容,因此字符串中可能包含空格或其他特殊字符,因此appendQueryParameter()还会将您的字符串转换为有效的HTML查询参数.因此,如果我搜索& = /?,则会得到查询参数q=%26+%3D+%2F%3F.

This is similar for the q=githubSearchQuery, but since this is user input, there may be spaces or other special characters in the string, so appendQueryParameter() also mangles your string into valid HTML query parameters. So, if I search for & = /?, I get the query parameter q=%26+%3D+%2F%3F.

顺便说一句,q=部分不是标准,只是查询约定(Google也使用该约定),而sort=stars部分是特定于GitHub的选项.

By the way, the q= part is not a standard, just a convention for queries (Google uses that too), and sort=stars is an option specific to GitHub.

这篇关于建立网址-两行混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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