【问题标题】:Using an api (rito api) with java (android)将 api (rito api) 与 java (android) 一起使用
【发布时间】:2017-04-03 19:12:31
【问题描述】:

我有这个网址:https://euw.api.riotgames.com/api/lol/EUW/v1.4/summoner/by-name/motschel123?api_key=RGAPI-6A9B210B-31F9-4AD9-8393-1CF3DA6E10EF

我想获取网站上的数据:

try {
    URL url = new URL("https://euw.api.riotgames.com/api/lol/" + region + "/v1.4/summoner/by-name/" + summonerN + "?api_key=" + apiCode);
    parseJSON(url);
} catch (MalformedURLException e) {
    Toast.makeText(getBaseContext(), "Error creating URL",
                   Toast.LENGTH_LONG).show();
} catch (Exception e) {
    Toast.makeText(getBaseContext(), "Error reading URL",
                   Toast.LENGTH_LONG).show();
}

还有函数parseJSON(URL url)

public void parseJSON(URL url) throws Exception{
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null)
        Toast.makeText(getBaseContext(), inputLine,
                       Toast.LENGTH_LONG).show();
    in.close();
}

但它不起作用,只会抛出Exception。 我该如何解决?

【问题讨论】:

  • 使用retrofi库,很容易从动态url中获取数据
  • @Akashkumar 改造?
  • 在下面找到答案。我提供了

标签: java android json


【解决方案1】:

您可以使用 Retrofit 库从 URL 或动态 URL 检索和解析数据。您也通过变量提供了基本 URL 和不同的部分。您必须创建正在接收的 vbarialbe 的 getter setter。

请按照本教程 Retrofit tutorial

Retrofit with QUery URL

你可以通过这个网站创建getter setter

create gettter and setter

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    相关资源
    最近更新 更多