【问题标题】:Using retrofit to get url from different relative paths使用改造从不同的相对路径获取 url
【发布时间】:2019-04-12 04:38:55
【问题描述】:

我正在尝试为每个客户的站点获取 CompanyEndpoint,但我对在界面上使用改造感到困惑。

这是我目前所拥有的:

CompanyName : "company1"
CompanyEndpoint : "https://example.com"
IdentityEndpoint : "https://example.com/identity"
AppLoginMode : "Anonymous"

AppRouterApi.java

public interface AppRouterApi {

    @GET("api/sites/{CompanyName}")
    Call<Company> getCompanyName (@Url  String companyName);


}

公司.java

public class Company {

    String Endpoint;

    public String getEndpoint() {
        return endpoint;
    }
}

MainActivity.java

 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://example.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        appRouterApi = retrofit.create(AppRouterApi.class);


        getCompany();


    }

    private void getCompany(){
        retrofit2.Call<Company> companyRequest = appRouterApi.getCompanyName(); //Error here saying a string cant be applied to ()
        companyRequest.enqueue(new retrofit2.Callback<Company>() {
            @Override
            public void onResponse(retrofit2.Call<Company> call, retrofit2.Response<Company> response) {

                if(!response.isSuccessful()){
                    textViewResult.setText("Code:" + response.code());
                    return;
                }

                Company company = response.body();
                String content = "";
                content += "Url" + company.getEndpoint();
                textViewResult.setText(content);


            }

            @Override
            public void onFailure(retrofit2.Call<Company> call, Throwable t) {

            }
        });
    }

https://example/sites/{companyName}

所以如果我搜索: https://example/sites/company1

JSON 将有一个对象,我需要获取端点 URL 值,即:https://company1.com

编辑:我的 textViewReslt 返回 403

【问题讨论】:

标签: android json dynamic gson retrofit2


【解决方案1】:

据我所知,有几件事正在发生。让我把它分成几块。

首先,您将注释 @Path 与注释 @Url 混淆了。它们服务于不同的目的。

当您想将一些路径格式化为 @GET 等注释内的 url 时,您可以使用 @Path

public interface AppRouterApi {
  @GET("api/sites/{CompanyName}")
  Call<Company> getCompanyName (@Path("CompanyName")  String companyName);
}

这个接口会将传递给getCompanyName的参数格式化为路径的一部分。调用getCompanyName("foo") 将调用端点"https://example.com/api/sites/foo"

当您想简单地调用该 url 时,您使用 @Url。在这种情况下,您只需使用 http 方法注释接口方法。例如,

public interface AppRouterApi {
  @GET
  Call<Company> getCompanyName (@Url String url);
}

然后您必须使用整个 url 调用该方法。要调用与以前相同的 URL,您必须调用 getCompanyName("https://example.com/api/sites/foo")

这是这两个注解在用法上的主要区别。您在文本视图中看到 null 的原因是因为您的模型的属性名称与 json 不匹配。您有 2 个选项。

首先,您可以将模型更改为:

public class Company {

   String CompanyEndpoint;

   public String getEndpoint() {
     return endpoint;
   }
}

CompanyEndpoint 与您在 json 中的名称完全相同。另一种方法是告诉您的 json 序列化程序您要使用什么名称。由于您使用的是gson,因此您可以像这样使用@SerializedName

public class Company {

   @SerializedName("CompanyEndpoint")
   String Endpoint;

   public String getEndpoint() {
     return endpoint;
   }
}

@SerializedName("CompanyEndpoint") 告诉gson 在序列化和反序列化时使用哪个名称。

本质上,您有两个选择。您可以使用端点或公司名称。如果您不希望域发生变化,我建议您使用带有@Path 注释的第一种方法。这就是 Retrofit 通常会做的事情,就个人而言,我认为它比传递 url 更容易处理。我的建议是,使用如下模型:

public class Company {

   @SerializedName("CompanyName")
   String name;

   public String getName() {
     return name;
   }
}

这将让您访问公司的名称属性并致电getCompanyName(company.getName())。 Retrofit 会将公司名称格式化到路径中,然后调用正确的 url。

【讨论】:

  • 工作完美,非常感谢。如果我想在运行时更改相对 url 怎么办。即在我的应用程序中,我为我的公司输入了一个代码,让我们说“comany1”,然后转到“example.com/api/sites/company1”,它请求端点 url,即“company1.com”。为每个重复我的 getCompany() 方法。公司似乎效率不高
  • 那么也许你应该考虑使用@Url的方法
猜你喜欢
  • 2020-07-20
  • 1970-01-01
  • 2011-05-29
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2011-12-11
  • 2011-04-10
相关资源
最近更新 更多