Search code examples
javaandroidretrofit

How to call api with dynamic parameter using retrofit Android?


I am trying to POST data to the server using retrofit library. Api includes the dynamic number of parameter like:

https:xyz.com/skill-add?skill[0]=10&skill1=11&skill[2]=12&skill[3]=267

here is postman SS:

enter image description here

I dont know how to use api like this one.

Can you please help me out?


Solution

  • Please try the below 3 way in interface with @QueryMap Map<String, String> params prefer to send dynamic data may help you

         public class ApiClient {
            
                    static WebApiService webApiService;
                    public static WebApiService getWebApiService(){
                        if(webApiService == null){
                            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
                            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
                            try {
                                httpClient.addInterceptor(new Interceptor() {
                                    @Override
                                    public Response intercept(Interceptor.Chain chain) throws IOException {
                                        Request original = chain.request();
                                        Request.Builder requestBuilder = original.newBuilder()
                                                .addHeader("Authorization","Bearer "+getUserAccessToken())
                                                .addHeader("access-token",getUserAccessToken())
                                                .addHeader("device-type", "android")
                                        Request request = requestBuilder.build();
                                        return chain.proceed(request);
                                    }
                                });
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                
                            OkHttpClient okHttpClient = httpClient.addInterceptor(interceptor).connectTimeout(60, TimeUnit.SECONDS).
                                    readTimeout(60, TimeUnit.SECONDS).
                                    writeTimeout(60, TimeUnit.SECONDS)
                                    .build();
                
                            String baseUrl = "https:xyz.com/";//put this in build.gradel and get from build config
                            Retrofit retrofit = new Retrofit.Builder()
                                    .client(okHttpClient)
                                    .baseUrl(baseUrl)
                                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                                    .addConverterFactory(GsonConverterFactory.create())
                                    .build();
                            webApiService = retrofit.create(WebApiService.class);
                        }
                        return webApiService;
                    }
                }
        
        
        
        
            public interface WebApiService {
            
                @POST("skill-add")
                Observable<Response<ResponseBody>> addskill(@Body Map<String, String> params);
            
    
     
                @POST("skill-add")
                Observable<Response<ResponseBody>> addskill(@QueryMap Map<String, String> params);
    
    
            
                @POST("skill-add")
                Observable<Response<ResponseBody>> addskill(@Query("skill[0]") int skill0,@Query("skill[1]") int skill1,@Query("skill[2]") String skill2);
            
            }
        
        
               try {
                    CompositeDisposable compositeDisposable = new CompositeDisposable();
                    compositeDisposable.add(ApiClient.getWebApiService().addskill(/*parameterhhere in your way*/)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(this::handleResults, this::handleError));
        
                } catch (Exception e) {
        
                    e.printStackTrace();
                }
    
    
    
    
    
       HashMap<String,String> params = new HashMap<>();
       int i = 0;
       params.put("skill["+i+"]","0");