0

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

public interface AppRouterApi {

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


}

Company.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}

So if I search for: https://example/sites/company1

The JSON will have one object and I need to get the endpoint URL value which would be: https://company1.com

Edit: My textViewReslt is returning 403

liam
  • 15
  • 1
  • 6
  • 1
    Possible duplicate of [Retrofit 2 - Dynamic URL](https://stackoverflow.com/questions/32559333/retrofit-2-dynamic-url) – pirho Nov 08 '18 at 17:45

1 Answers1

1

There are several things going on as far as I can tell. Let me break it into chunks.

First thing is you're confusing the annotation @Path with the annotation @Url. They serve different purposes.

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

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

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

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").

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 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") tells gson which name to use while serializing and deserializing.

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

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.

Fred
  • 16,367
  • 6
  • 50
  • 65
  • Worked perfectly, much appreciated. What if i want to change the relative url on runtime. i.e. on my app I enter a code for my company, lets say 'comany1' this then goes to 'https://example.com/api/sites/company1' which requests the endpoint url which would be 'https://company1.com'. Repeating my getCompany() method for each . company doesnt seem efficient – liam Nov 09 '18 at 12:27
  • Then perhaps you should consider the approach with `@Url` – Fred Nov 09 '18 at 16:38