2014-04-24 14 views
15

में डायनामिक पथ मैं http://192.168.1.64:5050/api/{api_key}/updater.info जैसे संसाधन तक पहुंचने का प्रयास कर रहा हूं।रेट्रोफिट

मैं गतिशील रूप से api_key पैरामीटर कैसे सेट करूं? मैंने सफलता के बिना RequestInterceptor का उपयोग करने का प्रयास किया है जहां बेस यूआरएल http://192.168.1.64:5050/api/{api_key} है।

@Override 
public void intercept(RequestFacade request) { 
    request.addPathParam("api_key", apiKey); 
} 

क्या कोई अन्य विकल्प हैं?

उत्तर

14

पथ प्रतिस्थापन एपीआई एंडपॉइंट के बेस यूआरएल के अंदर नहीं होता है, विधि पर केवल संबंधित यूआरएल स्ट्रिंग। मुझे लगता है कि आप अपने इंटरफ़ेस विधि घोषणाओं में से प्रत्येक पर सापेक्ष URL को उपसर्ग नहीं करना चाहते हैं।

जबकि खराब शब्दों में, Endpoint राज्यों के जावाडोक:

कॉलर्स हमेशा नवीनतम मूल्यों के लिए उदाहरण के बजाय लौटे मूल्यों कैशिंग से परामर्श करना चाहिए।

इसका मतलब है कि प्रत्येक अनुरोध के लिए Endpoint उदाहरण बेस यूआरएल के मूल्य के लिए परामर्श दिया जाएगा।

आप एक कस्टम Endpoint कार्यान्वयन जिस पर आप API कुंजी मूल्य बदल सकते हैं आपूर्ति कर सकते हैं: उदाहरण के लिए पथ पैरामीटर प्रत्येक अनुरोध के लिए URL में एक ही स्थिति में नहीं है, तो

public final class FooEndpoint implements Endpoint { 
    private static final String BASE = "http://192.168.1.64:5050/api/"; 

    private String url; 

    public void setApiKey(String apiKey) { 
    url = BASE + apiKey; 
    } 

    @Override public String getName() { 
    return "default"; 
    } 

    @Override public String getUrl() { 
    if (url == null) throw new IllegalStateException("API key not set."); 
    return url; 
    } 
} 
+0

क्या आप [एकाधिक एपीआई एंड पॉइंट्स को संबोधित करने के लिए मेरे प्रयास पर ध्यान दें] (http://stackoverflow.com/questions/27399633/how-to-address-multiple-api-end-points-using-retrofit)? – JJD

2

,, http://endpoint/blah/{apiKey} और http://endpoint/blah/blah/{apiKey}/blah, आप निम्न कार्य कर सकते हैं।

अपने APIService इंटरफ़ेस में

@GET(/blah/{apiKey}) 
    void getFoo(Callback<Object> callback); 

    @GET(/blah/blah/{apiKey}/blah) 
    void getFooBlah(Callback<Object> callback); 
अपने ApiClient कक्षा में

फिर एक RequestInterceptor

private static APIService sAuthorizedApiService; 
private static Gson gson; 

static { 
    gson = new GsonBuilder().setPrettyPrinting() 
      .create(); 
} 


public static synchronized APIService getApiClient(final Context context) { 
    if (sAuthorizedApiService == null) { 
     RequestInterceptor requestInterceptor = new RequestInterceptor() { 
      @Override 
      public void intercept(RequestFacade request) { 
       request.addPathParam("apiKey", DataProvider.getInstance(context).getApiKey(); 
      } 
     }; 

     RestAdapter restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL) 
       .setClient(new OkClient(new OkHttpClient())) 
       .setEndpoint("http://endpoint") 
       .setRequestInterceptor(requestInterceptor) 
       .setConverter(new GsonConverter(gson)) 
       .build(); 
     sAuthorizedApiService = restAdapter.create(GMAuthorizedApiService.class); 
    } 
    return sAuthorizedApiService; 
} 
24

उपयोग इस बनाने के लिए:

@PUT("/path1/path2/{userId}") 
void getSomething(
     @Path("userId") String userId 
); 

और आप इस तरह विधि कॉल:

String userId = "1234"; 
service.getSomething(userId);