Retrofit 2 - 동적 URL
Retrofit 2를 사용하면 다음과 같은 서비스 메서드의 주석에 전체 URL을 설정할 수 있습니다.
public interface APIService {
@GET("http://api.mysite.com/user/list")
Call<Users> getUsers();
}
그러나 제 앱에서는 컴파일 시 웹 서비스의 URL을 알 수 없고 앱에서 다운로드한 파일로 검색하므로 전체 동적 URL로 레트로핏 2를 사용할 수 있는 방법이 궁금합니다.
다음과 같이 전체 경로를 설정하려고 했습니다.
public interface APIService {
@GET("{fullUrl}")
Call<Users> getUsers(@Path("fullUrl") fullUrl);
}
new Retrofit.Builder()
.baseUrl("http://api.mysite.com/")
.build()
.create(APIService.class)
.getUsers("http://api.mysite.com/user/list"); // this url should be dynamic
.execute();
그러나 여기서 Retrofit은 경로가 실제로 전체 URL이 아니라고 보고 다운로드를 시도하고 있습니다.http://api.mysite.com/http%3A%2F%2Fapi.mysite.com%2Fuser%2Flist
이렇게 동적인 url로 Retrofit을 어떻게 사용할 수 있는지 힌트가 있습니까?
감사해요.
당신은 그것을 잘못된 방식으로 사용하고 있는 것 같습니다.다음은 변경 로그에서 발췌한 내용입니다.
새로 만들기: @URL 매개 변수 주석을 사용하면 끝점에 대한 전체 URL을 전달할 수 있습니다.
따라서 인터페이스는 다음과 같습니다.
public interface APIService {
@GET
Call<Users> getUsers(@Url String url);
}
url의 일부만 교체하고 싶었으며, 이 솔루션을 사용하면 url 전체를 전달할 필요가 없고 동적인 부분만 전달할 수 있습니다.
public interface APIService {
@GET("users/{user_id}/playlists")
Call<List<Playlist> getUserPlaylists(@Path(value = "user_id", encoded = true) String userId);
}
암호화된 플래그를 사용할 수 있습니다.@Path
주석:
public interface APIService {
@GET("{fullUrl}")
Call<Users> getUsers(@Path(value = "fullUrl", encoded = true) String fullUrl);
}
- 이를 통해 의 교체를 방지할 수 있습니다.
/
와 함께%2F
. - 그것은 당신을 구원하지 못할 것입니다.
?
로 대체됨%3F
, 그러나 여전히 동적 쿼리 문자열을 전달할 수 없습니다.
Retrofit 2.0.0-beta2 기준, 이 URL에서 JSON을 응답하는 서비스가 있는 경우: http://myhost/mypath
다음이 작동하지 않습니다.
public interface ClientService {
@GET("")
Call<List<Client>> getClientList();
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myhost/mypath")
.addConverterFactory(GsonConverterFactory.create())
.build();
ClientService service = retrofit.create(ClientService.class);
Response<List<Client>> response = service.getClientList().execute();
하지만 이건 괜찮습니다.
public interface ClientService {
@GET
Call<List<Client>> getClientList(@Url String anEmptyString);
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myhost/mypath")
.addConverterFactory(GsonConverterFactory.create())
.build();
ClientService service = retrofit.create(ClientService.class);
Response<List<Client>> response = service.getClientList("").execute();
다음을 사용할 수 있습니다.
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);
자세한 내용은 https://square.github.io/retrofit/ 설명서를 참조하십시오.
MVVM(Retrofit)에서 Get and Post 메서드를 사용한 동적 URL
Retrofit 서비스 인터페이스:
public interface NetworkAPIServices {
@POST()
Observable<JsonElement> executXYZServiceAPI(@Url String url,@Body AuthTokenRequestModel param);
@GET
Observable<JsonElement> executeInserInfo(@Url String url);
MVVM 서비스 클래스:
public Observable<JsonElement> executXYZServiceAPI(ModelObject object) {
return networkAPIServices.authenticateAPI("url",
object);
}
public Observable<JsonElement> executeInserInfo(String ID) {
return networkAPIServices.getBank(DynamicAPIPath.mergeUrlPath("url"+ID)));
}
그리고 Retrofit 클라이언트 클래스
@Provides
@Singleton
@Inject
@Named("provideRetrofit2")
Retrofit provideRetrofit(@Named("provideRetrofit2") Gson gson, @Named("provideRetrofit2") OkHttpClient okHttpClient) {
builder = new Retrofit.Builder();
if (BaseApplication.getInstance().getApplicationMode() == ApplicationMode.DEVELOPMENT) {
builder.baseUrl(NetworkURLs.BASE_URL_UAT);
} else {
builder.baseUrl(NetworkURLs.BASE_URL_PRODUCTION);
}
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
builder.client(okHttpClient);
builder.addConverterFactory(GsonConverterFactory.create(gson));
return builder.build();
}
예를 들어, 이것은 https://gethelp.wildapricot.com/en/articles/549-changing-your
baseURL : https://gethelp.wildapricot.com
나머지 @Url: /en/articles/549-changing-your(복고 서비스 클래스에서 통과)
1단계
Please define a method in Api interface like:-
@FormUrlEncoded
@POST()
Call<RootLoginModel> getForgotPassword(
@Url String apiname,
@Field(ParameterConstants.email_id) String username
);
단계-2 모범 사례를 위해 레트로핏 인스턴스에 대한 클래스를 정의합니다:-
public class ApiRequest {
static Retrofit retrofit = null;
public static Retrofit getClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.addInterceptor(logging)
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(URLConstants.base_url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
} 3단계는 활동에서 다음과 같이 정의합니다.
final APIService request =ApiRequest.getClient().create(APIService.class);
Call<RootLoginModel> call = request.getForgotPassword("dynamic api
name",strEmailid);
레트로핏코틀린으로 작성된 도우미 라이브러리를 통해 몇 줄의 코드를 사용하여 API 호출을 할 수 있으며, 모든 호출에서 헤더와 파라미터 등 다양한 URL을 사용할 수 있습니다.
다음과 같이 응용프로그램 클래스에 여러 URL을 추가합니다.
class Application : Application() {
override fun onCreate() {
super.onCreate()
retrofitClient = RetrofitClient.instance
//api url
.setBaseUrl("https://reqres.in/")
//you can set multiple urls
// .setUrl("example","http://ngrok.io/api/")
//set timeouts
.setConnectionTimeout(4)
.setReadingTimeout(15)
//enable cache
.enableCaching(this)
//add Headers
.addHeader("Content-Type", "application/json")
.addHeader("client", "android")
.addHeader("language", Locale.getDefault().language)
.addHeader("os", android.os.Build.VERSION.RELEASE)
}
companion object {
lateinit var retrofitClient: RetrofitClient
}
}
그런 다음 필요한 URL을 통화에 사용합니다.
retrofitClient.Get<GetResponseModel>()
//set base url
.setBaseUrlKey("example")
//set path
.setPath("api/users/2")
//set url params Key-Value or HashMap
.setUrlParams("KEY","Value")
.setResponseHandler(GetResponseModel::class.java,
object : ResponseHandler<GetResponseModel>() {
override fun onSuccess(response: Response<GetResponseModel>) {
super.onSuccess(response)
//handle response
}
}).run(this)
자세한 내용은 설명서 참조
이미 코드 설정이 되어 있고 다른 인터페이스를 변경하지 않으려면 이 링크에 설명된 솔루션을 사용합니다.요점은 방법입니다.changeApiBaseUrl
URL을 업데이트하고 Retrofit Builder를 다시 만듭니다.
public class ServiceGenerator {
public static String apiBaseUrl = "http://futurestud.io/api";
private static Retrofit retrofit;
private static Retrofit.Builder builder =
new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(apiBaseUrl);
private static OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
// No need to instantiate this class.
private ServiceGenerator() {
}
public static void changeApiBaseUrl(String newApiBaseUrl) {
apiBaseUrl = newApiBaseUrl;
builder = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(apiBaseUrl);
}
public static <S> S createService(Class<S> serviceClass, AccessToken token) {
String authToken = token.getTokenType().concat(token.getAccessToken());
return createService(serviceClass, authToken);
}
// more methods
// ...
}
다음과 같이 사용할 수 있습니다.
public class DynamicBaseUrlActivity extends AppCompatActivity {
public static final String TAG = "CallInstances";
private Callback<ResponseBody> downloadCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_upload);
downloadCallback = new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG, "server contacted at: " + call.request().url());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d(TAG, "call failed against the url: " + call.request().url());
}
};
// first request
FileDownloadService downloadService = ServiceGenerator.create(FileDownloadService.class);
Call<ResponseBody> originalCall = downloadService.downloadFileWithFixedUrl();
originalCall.enqueue(downloadCallback);
// change base url
ServiceGenerator.changeApiBaseUrl("http://development.futurestud.io/api");
// new request against new base url
FileDownloadService newDownloadService = ServiceGenerator.create(FileDownloadService.class);
Call<ResponseBody> newCall = newDownloadService.downloadFileWithFixedUrl();
newCall.enqueue(downloadCallback);
}
}
언급URL : https://stackoverflow.com/questions/32559333/retrofit-2-dynamic-url
'source' 카테고리의 다른 글
가변 인수는 gcc에서 어떻게 구현됩니까? (0) | 2023.10.26 |
---|---|
현재 카테고리의 워드프레스 게시물만 나열합니다. (0) | 2023.10.26 |
목표-C에서 프로토콜/딜러를 확장하는 방법은 무엇입니까? (0) | 2023.10.26 |
wp_nav_메뉴 항목에 특징 이미지 추가 (0) | 2023.10.26 |
typedef 포인터 상수 이상함 (0) | 2023.10.26 |