Using Retrofit to access API with key / id

I didn't really find any clear documentation on the matter, but I was wondering if someone could point me to something to use Retrofit (or OKHTTP or something if that's more appropriate) to pull a JSON stream from the API, which requires an API identifier and an API key (example of accessing data from a terminal: curl -v -H "app_id: appid" -H "app_key: appkey" -X GET " http://data.leafly.com/strains/blue- dream ").

I checked the official documentation on the Square website, but if there is anything else it might help me to just pull this data, which would be awesome.

Thank!


Final answer

RestAdapter.Builder builder= new RestAdapter.Builder()
            .setEndpoint( "http://data.leafly.com/" )
            .setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestInterceptor.RequestFacade request) {
                    request.addHeader("app_id", "id");
                    request.addHeader("app_key", "key");
                }
            });

    final RestAdapter restAdapter = builder.build();

new Thread( new Runnable() {
        @Override
        public void run() {
            LeaflyService service = restAdapter.create(LeaflyService.class);
            final Strain strain = service.data("blue-dream");

            runOnUiThread( new Runnable() {
                @Override
                public void run() {
                    mText.setText( strain.getDescription() );
                }
            } );
        }
    } ).start();

      

plus a deformation model that just fits the data.

+3


source to share


2 answers


To send the id and key, you need to change the HTTP headers. It looks like you can do it with the @Header

annotation
in the Appendix.



+4


source


the answer above didn't work for me, here is my solution:

    RestAdapter.Builder adapterBuilder = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL)
            .setEndpoint(BACKEND_URL).setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestInterceptor.RequestFacade request) {
                    request.addQueryParam("apikey", apiKey);
                }
            });
    service = adapterBuilder.build().create(BackendInterface.class);

      



@POST("/getall")
void getIdeaList( Callback<GetAllIdeaResponse> callback);

      

+2


source







All Articles