4

How to make this query which I will mention below?

Method @GET. Query should looks like this:

/top40?data={"ranking":"world"} /top40?data={"ranking":"country"}

@GET("/api/top40")
    Call<FamousTop40Model> getStatus(
        // what should be there?
    );

    class Factory {
        private static FamousTop40Api service;

        public static FamousTop40Api getInstance() {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            service = retrofit.create(FamousTop40Api.class);

            return service;
        }
    }

Can you guys help me?

EDIT: + I should have accessKey in the header.

y07k2
  • 1,898
  • 4
  • 20
  • 36
  • You want to make Get or Put request? – Dhruvi Jul 05 '16 at 11:21
  • Get request, but in received API it is said I should send json to receive data. It is even possible to send json params in Get? – y07k2 Jul 05 '16 at 11:24
  • You can follow the below answer.@y07k2 – Lips_coder Jul 05 '16 at 11:24
  • 1
    @y07k2 Yes it is possible to send json but I am not sure if it can be send using GET, but refer this link to send the json using POST http://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request – Dhruvi Jul 05 '16 at 11:27

3 Answers3

2

That helps me:

public interface FamousTop40Api {
    @GET("/api/top40")
    Call<FamousTop40Model> getStatus(
            @Query("data") String ranking
    );

    class Factory {
        private static FamousTop40Api service;

        public static FamousTop40Api getInstance() {
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                  Request original = chain.request();

                  Request request = original.newBuilder()
                          .header("accessKey", MainActivity.ACCESS_KEY)
                          .method(original.method(), original.body())
                          .build();

                  return chain.proceed(request);
                }
            });

            OkHttpClient client = httpClient.build();

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();

            service = retrofit.create(FamousTop40Api.class);

            return service;
        }
    }
}

So you need to add @Query and accessKey in header with OkHttpClient

and

FamousTop40Api.Factory.getInstance().getStatus("{\"ranking\":\"country\"}").enqueue();
y07k2
  • 1,898
  • 4
  • 20
  • 36
0

You can call your GET method like this:

public interface EndPointInterface{
@GET(ProximityConstants.URL_FETCH_STORE_INFO)
    Call<Store> getStoreInfoByBeaconUUID(@Query(ProximityConstants.BEACON_UUID) String beaconUUID);

}

To Call the GET method webservice proceed like this:

 Call<StoreSettings> call = apiService.getStoreSettings(mStore.getStoreID(), mCustomer.getCustId(),
                mStore.getBeaconID(), ProximityConstants.SETTINGS_CUST_TYPE, ProximityConstants.ACTION_STORE_VISIT);
        call.enqueue(new Callback<StoreSettings>() {
            @Override
            public void onResponse(Call<StoreSettings> call, Response<StoreSettings> response) {
                ProximityUtil.writeLog(TAG, "Received store settings");
                mStoreSettings = response.body();
                SharedPreferences.Editor editor = mAppPreferences.edit();
                editor.putString(ProximityConstants.SETTINGS_OBJ_STORE_SETTINGS, new Gson().toJson(mStoreSettings));
                editor.commit();
                Intent intent = new Intent(BeaconScanService.this, ProximityWelcomeActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                BeaconScanService.this.startActivity(intent);
            }

            @Override
            public void onFailure(Call<StoreSettings> call, Throwable t) {
                Toast.makeText(BeaconScanService.this, "Error: " + t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
Lips_coder
  • 686
  • 1
  • 5
  • 17
  • `@Query("ranking") String ranking` will create this request with `?data={...}`? Where can i add header? – y07k2 Jul 05 '16 at 11:30
  • didn't got you ,in your get service do you need pass parameters or header? – Lips_coder Jul 05 '16 at 11:35
  • I didn't get it. `accessKey` in header and `data` json which contains `ranking` String value in a parameters. – y07k2 Jul 05 '16 at 11:45
-3

Try as below: Create Class Task.java:

public class Task {  
private String ranking;

public Task(String ranking) {
    this.ranking = ranking;
}
}

And do as below:

public interface TaskService {  

@POST("/top40?data=")
Call<Top40Model> getStatus(@Body Task task);

}

And use as below:

Task task = new Task("world");  
Task task = new Task("country");  

Call<Top40Model> call=new Factory.getInstance().getStatus(task);
call.enqueue(new Callback<Top40Model>() {});
Dhruvi
  • 1,971
  • 2
  • 10
  • 18
  • `"data={"ranking":"{ranking}"}"` how can I replace `ranking` with String value? cause e.g. this url is valid in Postman `http://5.135.147.222:8400/api/top40?data={"ranking":"world"}`. Notice, that `{..}` contains all parameters – y07k2 Jul 05 '16 at 13:21
  • 1
    Got error: `IllegalArgumentException: URL query string "data={"ranking":"{ranking}"}" must not have replace block. For dynamic query parameters use @Query.` when using @Query. I think cause brackets which must be there... – y07k2 Jul 05 '16 at 13:24
  • 2
    He said GET, not POST. – nasch Sep 16 '17 at 22:28
  • yep, the body json is for POST requests, but the question was asking about json object in URL – Filip Luchianenco Sep 24 '18 at 19:38