使用改造从不同的相对路径获取网址 [英] Using retrofit to get url from different relative paths
问题描述
我试图为每个客户的站点获取CompanyEndpoint,但是我对在界面上使用改造感到困惑.
I am trying to get the CompanyEndpoint for each client's site but I am confused with the use of retrofit on the interface.
这是我到目前为止所拥有的:
Here's what I have so far:
CompanyName : "company1"
CompanyEndpoint : "https://example.com"
IdentityEndpoint : "https://example.com/identity"
AppLoginMode : "Anonymous"
AppRouterApi.java
AppRouterApi.java
public interface AppRouterApi {
@GET("api/sites/{CompanyName}")
Call<Company> getCompanyName (@Url String companyName);
}
Company.java
Company.java
public class Company {
String Endpoint;
public String getEndpoint() {
return endpoint;
}
}
MainActivity.java
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/{companyName}
因此,如果我搜索: https://example/sites/company1
JSON将具有一个对象,我需要获取以下端点URL值: https://company1.com
The JSON will have one object and I need to get the endpoint URL value which would be: https://company1.com
我的textViewReslt返回403
My textViewReslt is returning 403
推荐答案
据我所知,有几件事正在发生.让我把它分成几块.
There are several things going on as far as I can tell. Let me break it into chunks.
第一件事是您将注释 @Path
与注释 @Url
混淆了.它们有不同的用途.
First thing is you're confusing the annotation @Path
with the annotation @Url
. They serve different purposes.
要在 @GET
之类的批注中将路径的一部分格式格式化为url时,请使用 @Path
.
You use @Path
when you want to format a bit of the path into the url inside the annotations like @GET
.
public interface AppRouterApi {
@GET("api/sites/{CompanyName}")
Call<Company> getCompanyName (@Path("CompanyName") String companyName);
}
此接口将格式化作为路径一部分传递给 getCompanyName
的参数.调用 getCompanyName("foo")
将调用端点"https://example.com/api/sites/foo"
.
This interface will format the argument passed to getCompanyName
as part of the path. Calling getCompanyName("foo")
will call the endpoint "https://example.com/api/sites/foo"
.
当您只想调用该URL时,可以使用 @Url
.在这种情况下,只用http方法注释接口方法.例如,
You use @Url
when you want to simply call that url. In this case, you only annotate the interface method with the http method. For example,
public interface AppRouterApi {
@GET
Call<Company> getCompanyName (@Url String url);
}
然后,您将必须使用整个URL来调用该方法.要调用与之前相同的网址,您必须调用 getCompanyName("https://example.com/api/sites/foo")
.
You then would have to call the method with the entire url. To call the same url as before you'd have to call getCompanyName("https://example.com/api/sites/foo")
.
这是这两个注释之间用法的主要区别.之所以在文本视图中看到null是因为模型的属性名称与json不匹配.您有2个选择.
This is the main difference of usage between these 2 annotations. The reason why you're seeing null in your text view is because you're model's attribute name doesn't match the json. You have 2 options.
首先,您可以将模型更改为:
First, you can change the model to:
public class Company {
String CompanyEndpoint;
public String getEndpoint() {
return endpoint;
}
}
CompanyEndpoint
是与json中完全相同的名称.另一种方法是告诉json序列化器您要使用的名称.由于您使用的是 gson
,因此可以这样使用 @SerializedName
:
CompanyEndpoint
is the exact same name as you have in the json. Another approach, is to tell your json serializer what name you want to use. Since you're using gson
, you can use @SerializedName
like so:
public class Company {
@SerializedName("CompanyEndpoint")
String Endpoint;
public String getEndpoint() {
return endpoint;
}
}
@SerializedName("CompanyEndpoint")
告诉 gson
在序列化和反序列化时使用哪个名称.
@SerializedName("CompanyEndpoint")
tells gson
which name to use while serializing and deserializing.
从本质上讲,您有2个选择.您可以使用端点或公司名称.如果您不希望域发生变化,建议您将第一种方法与 @Path
批注一起使用.这是Retrofit通常要做的事情,就个人而言,我认为它比传递URL更容易处理.我的建议是,使用类似以下的模型:
In essence, you have 2 options. You either use the endpoint, or the company's name. If you don't expect the domain to change, I'd suggest using the first approach with the @Path
annotation. This is what it's usually done with Retrofit and personally, I think it's easier to handle than passing urls around. My suggestion is, use a model like:
public class Company {
@SerializedName("CompanyName")
String name;
public String getName() {
return name;
}
}
这将允许您访问公司的名称属性,并调用 getCompanyName(company.getName())
.翻新会将公司名称格式化为路径,然后您将调用正确的网址.
This would let you access the company's name property and call getCompanyName(company.getName())
. Retrofit would format the company's name into the path and you'd call the right url.
这篇关于使用改造从不同的相对路径获取网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!