How to call API using Retrofit method , here we are looking how to parse web API using Retrofit and with Header.
APIInterface apiInterface = RetrofitService.createService(ApiInterface.class, "BASE_URL", false);
the "false" is your Header boolean , if your web api having Header like "Bearer" token so the boolean is "true".
//Retrofit service method for all api callingpublic static <S> S createService(Class<S> serviceClass, String serverURL, boolean withHeader) { Gson gson = new GsonBuilder().setLenient().create(); HttpLoggingInterceptor logging = new HttpLoggingInterceptor();logging.setLevel(HttpLoggingInterceptor.Level.BODY); httpClient = new OkHttpClient.Builder();httpClient.addInterceptor(logging); httpClient.connectTimeout(60, TimeUnit.SECONDS );httpClient.readTimeout(60, TimeUnit.SECONDS ); // httpClient.addNetworkInterceptor(new AddHeaderInterceptor());if (withHeader) { httpClient.addInterceptor(new Interceptor() { @Overridepublic Response intercept(Chain chain) throws IOException { final Request request = chain.request().newBuilder() //.addHeader("Accept", "application/json")//.addHeader("Authorization","Bearer "+ Activity_Login.token)//A1 update.addHeader("X-Requested-With", "XMLHttpRequest") .addHeader("Authorization", "Bearer "+"your token").build(); //SharedHelper.getKey(mContext, Str_RemeberLoginKey)
return chain.proceed(request); }});}
builder = new Retrofit.Builder().baseUrl(serverURL).addConverterFactory(GsonConverterFactory.create(gson)).addConverterFactory(ScalarsConverterFactory.create());Retrofit retrofit = builder.client(httpClient.build()).build(); return retrofit.create(serviceClass);}
Now , above is the method for api parsing .Make your pojo class using api response and put it in your code and i here give you a code for api parsing using Retrofit method.
private void callRegister() {
APIInterface reg_api = RetrofitService.createService(ApiInterface.class, "BASE_URL", true);
//str_fname,str_lname,str_phone
RegisterBody regmodel = new RegisterBody();
regmodel.setFirst_name(str_fname);
regmodel.setLast_name(str_lname);
regmodel.setCountry_code("+91");
regmodel.setMobile(str_phone);
regmodel.setPassword(str_password);
Call<RegisterModel> callAdvisoryApi = reg_api.getRegister(regmodel);
callAdvisoryApi.enqueue(new Callback<RegisterModel>() {
@Override
public void onResponse(Call<RegisterModel> call, Response<RegisterModel> response) {
RegisterModel objRegisterModel = response.body();
if (response.code() == 200) {
Toast.makeText(mContext,"Register Successfully..",Toast.LENGTH_LONG).show();
Intent myintent = new Intent(mContext, MainActivity.class);
startActivity(myintent);
} else {
Utilities.displayMessage(mContext, "Could not signup..Please try again");
}
}
@Override
public void onFailure(Call<RegisterModel> call, Throwable t) {
Toast.makeText(mContext, "Failed to load data" + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public interface ApiInterface {
@POST("/api/user/signup")
Call<RegisterModel> getRegister(@Body RegisterBody regmodelobj);
@POST("/api/user/login")
Call<LoginModel> postLogin(@Body LoginParameters params);
}
Below is the link for code of Retrofit api calling.
private void callRegister() {
APIInterface reg_api = RetrofitService.createService(ApiInterface.class, "BASE_URL", true);
//str_fname,str_lname,str_phone
RegisterBody regmodel = new RegisterBody();
regmodel.setFirst_name(str_fname);
regmodel.setLast_name(str_lname);
regmodel.setCountry_code("+91");
regmodel.setMobile(str_phone);
regmodel.setPassword(str_password);
Call<RegisterModel> callAdvisoryApi = reg_api.getRegister(regmodel);
callAdvisoryApi.enqueue(new Callback<RegisterModel>() {
@Override
public void onResponse(Call<RegisterModel> call, Response<RegisterModel> response) {
RegisterModel objRegisterModel = response.body();
if (response.code() == 200) {
Toast.makeText(mContext,"Register Successfully..",Toast.LENGTH_LONG).show();
Intent myintent = new Intent(mContext, MainActivity.class);
startActivity(myintent);
} else {
Utilities.displayMessage(mContext, "Could not signup..Please try again");
}
}
@Override
public void onFailure(Call<RegisterModel> call, Throwable t) {
Toast.makeText(mContext, "Failed to load data" + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public interface ApiInterface {
@POST("/api/user/signup")
Call<RegisterModel> getRegister(@Body RegisterBody regmodelobj);
@POST("/api/user/login")
Call<LoginModel> postLogin(@Body LoginParameters params);
}
No comments:
Post a Comment
Comments