Ramya BA
Ramya BA

Reputation: 123

Dynamic endpoints using Retrofit Get Method

This is my Request Url: http://getpincodes.info/api.php?pincode=560054 where pincode value is dynamic.

Here is my code:

public interface PincodeDetailsService 

@GET("")
Call<PincodeDetailsResponse> getPin(@Url String url);

}

ApiUtils :

public static final String PINCODE_URL ="http://getpincodes.info/";

   public static PincodeDetailsService getPincodeDetailsService() {
    return RetrofitClientInstance.getRetrofitInstance(PINCODE_URL).create(PincodeDetailsService.class);
}

how do i pass the other part while making an network call? Can anybody help me on this?

Upvotes: 0

Views: 168

Answers (2)

Bek
Bek

Reputation: 8471

First response header Content-Type is text/html change it to application/json. Create retrofit instance as below

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://getpincodes.info")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

ApiService apiService = retrofit.create(ApiService.class);

Create model like this.

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Info {

@SerializedName("pincode")
@Expose
private String pincode;
@SerializedName("city")
@Expose
private String city;
@SerializedName("district")
@Expose
private String district;
@SerializedName("state")
@Expose
private String state;

public String getPincode() {
return pincode;
}

public void setPincode(String pincode) {
this.pincode = pincode;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getDistrict() {
return district;
}

public void setDistrict(String district) {
this.district = district;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

}

Then in api interface add below code

@GET("/api.php")
Call<List<Info>> getInfo(@Query("pincode") String pincode);

Then call apiService.getInfo("560054")

Upvotes: 1

Vladimir Gladun
Vladimir Gladun

Reputation: 416

The signature should be something like this:

@GET("api.php")
Call<PincodeDetailsResponse> getPin(@Query("pincode") int pincode);

Then simply call it using

getPin(560054);

Upvotes: 1

Related Questions