안드로이드 개발/안드로이드
[안드로이드 개발] retrofit으로 텍스트 받아오기.
달님개발자
2023. 3. 9. 11:35
반응형
OpenSubtitles.com에서 자막을 받아오는 소스를 만들고 있다.
자막을 받아왔는데, "1"이란 숫자만 보인다. 짐작에 자막이 srt포맷이라 맨처음 ID를 보여주는거 같다.
구글링 해보니 GsonConverterFactory를 쓰지 말고 ScalarsConverterFactory를 사용하라고 한다.
A converter for strings and both primitives and their boxed types to text/plain bodies.
문자열이나 기본형을 텍스트형태 그대로 보여준다.
interface OpenSubtitleDownloadService {
@GET
Call<String> downloadFile(@Url String url);
}
public class OpenSubtitleApi {
private final OpenSubtitleDownloadService downloadService;
private static Retrofit downloadRetrofit;
public OpenSubtitleApi(Context context) {
this.downloadService = getDownloadRetrofitInstance(client).create(OpenSubtitleDownloadService.class);
}
private static Retrofit getDownloadRetrofitInstance(OkHttpClient client) {
final String baseUrl = "https://www.opensubtitles.com";
if (downloadRetrofit == null) {
downloadRetrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
return downloadRetrofit;
}
public void downloadSubtitle(String fileUrl, DalApiListener<String> listener) {
Call<String> call = downloadService.downloadFile(fileUrl);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
String content = response.body();
listener.onSuccess(content);
} else {
listener.onFailure("");
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
listener.onFailure("");
}
});
}
}
부르는 쪽은 이런씩으로 부른다.
String link = "https://www.opensubtitles.com/download/000006/subfile/10.Things.I.Hate.About.You.1999.1080p.BluRay.x264-OEM.ENG.srt";
openSubtitleApi.downloadSubtitle(link, new DalApiListener<String>() {
@Override
public void onSuccess(String response) {
binding.tvPreview.setText(response);
}
@Override
public void onFailure(String error) {
binding.tvPreview.setText("Error during download");
}
});
반응형