How To Post Json Using Retrofit 2 In Android?
I am new to android and I dont know how to POST using retrofit I have my own server which returns me data, I need to fetch that one This is how URL body looks like I have to send
Solution 1:
Follow these steps:
1.) Firstly create modal class (MyRequestClass) for your Post request from http://www.jsonschema2pojo.org/
2.) Also create modal (MyResponseClass) for your server response from same site.
3.) Set values you want to send to your server by creating object of MyRequestClass.
4.) Then hit api using this method in your interface:
@POST("/provider/filter")
Call<MyResponseClass> sendDataToServer(@Body MyRequestClass myModal);
Solution 2:
your response class is wrong. it should be :
Response{
private Data[] data;
public Data[] getData ()
{
return data;
}
publicvoidsetData (Data[] data)
{
this.data = data;
}
@Override
public String toString()
{
return"ClassPojo [data = "+data+"]";
}
publicclassData
{
private String id;
private String city_name;
public String getId ()
{
return id;
}
publicvoidsetId (String id)
{
this.id = id;
}
public String getCity_name ()
{
return city_name;
}
publicvoidsetCity_name (String city_name)
{
this.city_name = city_name;
}
@Override
public String toString()
{
return"ClassPojo [id = "+id+", city_name = "+city_name+"]";
}
}
}
retrofit call:
@POST("/lfs/city_name")
Call<Response> locData (@Body JsonRequestBean mRequest);
}
add value to keyword using setter method
Solution 3:
remove /. from POST Method as retrofit base url ends with /. so your url is now baseurl//lfs/city_name
@POST("lfs/city_name")<----- change
Call<JsonResponseBean> locData (@Body JsonRequestBean mRequest);
adding headers to request
publicclassRetrofitClient {
privatestaticfinalStringTAG="RetrofitClient";
publicstatic Retrofit getClient() {
Retrofitretrofit=null;
HttpLoggingInterceptorlogging=newHttpLoggingInterceptor();
// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.BuilderhttpClient=newOkHttpClient.Builder();
httpClient.addInterceptor(logging);
httpClient.addInterceptor(newInterceptor() {
@Overridepublic Response intercept(Interceptor.Chain chain)throws IOException {
Requestoriginal= chain.request();
// Request customization: add request headers
Request.BuilderrequestBuilder= original.newBuilder()
.header("Licence", BuildConfig.Licence); // <-- this is the important lineRequestrequest= requestBuilder.build();
return chain.proceed(request);
}
});
if (retrofit==null) {
Log.d(TAG, "getClient: base url "+ BuildConfig.base_url);
retrofit = newRetrofit.Builder()
.baseUrl(BuildConfig.base_url)
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
publicstatic Retrofit getClientWithAdminBaseUrl() {
Retrofitretrofit=null;
HttpLoggingInterceptorinterceptor=newHttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClientclient=newOkHttpClient.Builder().addInterceptor(interceptor).build();
if (retrofit==null) {
retrofit = newRetrofit.Builder()
.baseUrl(BuildConfig.admin_base_url)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Post a Comment for "How To Post Json Using Retrofit 2 In Android?"