一个调用方法的Java类

时间:2018-02-02 12:51:12

标签: android

这是NetworkUtils.class

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

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

这是MainActivity.class

public class MainActivity extends AppCompatActivity {

private EditText mSearchBoxEditText;
private TextView mUrlDisplayTextView;
private TextView mSearchResultsTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSearchBoxEditText = (EditText) findViewById(R.id.et_search_box);
    mUrlDisplayTextView = (TextView) findViewById(R.id.tv_url_display);
    mSearchResultsTextView = (TextView) findViewById(R.id.tv_github_search_results_json);
}

    private void makeGithubSearchQuery() {

    String githubQuery = mSearchBoxEditText.getText().toString();
    URL githubSearchUrl = NetworkUtils.buildUrl(githubQuery)
    mUrlDisplayTextView.setText(githubSearchUrl.toString());
    String githubSearchResults = null;
    try {
        githubSearchResults = NetworkUtils.getResponseFromHttpUrl(githubSearchUrl);
        mSearchResultsTextView.setText(githubSearchResults);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // TODO (4) Create a new GithubQueryTask and call its execute method, passing in the url to query
}

请注意,MainActivity.class使用此代码。

URL githubSearchUrl = NetworkUtils.buildUrl(githubQuery)

并且NetworkUtils.class使用此代码。

public static URL buildUrl(String githubSearchQuery) 

我确实相信 NetworkUtils.buildUrl(githubQuery)指的是buildUrl(String githubSearchQuery),实在令人困惑。我知道githubQuery的值将是mSearchBoxEditText.getText().toString();的输入,现在我想知道String githubSearchQuery的值是什么,它来自哪里?

2 个答案:

答案 0 :(得分:0)

githubSearchQuery 的值与 githubQuery 的值相同,因为它用于调用 buildUrl(String githubSearchQuery)方法。

答案 1 :(得分:0)

  

2使用相同方法的Java类

实际上标题本身是一个巨大的错误。 它们不同,方法不同。您可以在NetworkUtils中找到的是该方法的实际定义。在MainActivity中,您只是在调用它。

当您调用方法时,控件将转到定义。还会将一个参数副本传递给它(请注意,如果是对象,则传递引用的副本,而不是实际对象的副本。因此任何更改都会影响到两侧)。从定义部分,您可以使用新名称访问它。

  

但是不是githubQuery是私有类吗?

这不是私密的。它是一个本地的,意味着它的范围小于私有。但是,只要您有参考,就不会有问题。

相关问题