【问题标题】:Android : How to make GET request using Retrofit without parameter?Android:如何使用没有参数的 Retrofit 发出 GET 请求?
【发布时间】:2018-01-22 12:28:26
【问题描述】:

我正在调用 API 来获取国家/地区列表。响应采用JSON 格式,包含对象数组。然后我需要将响应填充到微调器中。那么如何在没有任何 @Query 或参数的情况下发送 get 请求然后读取响应?

我的 Json 是这种格式:

    [
    {
        "id": "1",
        "sortname": "AF",
        "name": "Afghanistan"
    },
    {
        "id": "2",
        "sortname": "AL",
        "name": "Albania"
    },
    {
        "id": "3",
        "sortname": "DZ",
        "name": "Algeria"
    },
    {
        "id": "4",
        "sortname": "AS",
        "name": "American Samoa"
    }
]

【问题讨论】:

  • 你现在用什么?
  • 关于改造的第一次研究

标签: android get retrofit2


【解决方案1】:

在您的 ApiService 界面中创建如下所示的 GET 请求:

@GET("url")
    Call<String> getCountries();

如果您不想发送任何参数,请不要在调用方法中添加任何参数。

它应该适合你。

【讨论】:

    【解决方案2】:

    参考:android-working-with-retrofit-http-library

    如下创建一个 pojo 类:

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("sortname")
    @Expose
    private String sortname;
    @SerializedName("name")
    @Expose
    private String name;
    
    public String getId() {
    return id;
    }
    
    public void setId(String id) {
    this.id = id;
    }
    
    public String getSortname() {
    return sortname;
    }
    
    public void setSortname(String sortname) {
    this.sortname = sortname;
    }
    
    public String getName() {
    return name;
    }
    
    public void setName(String name) {
    this.name = name;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 2020-10-08
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多