일모도원(日暮途遠) 개발자
[안드로이드 개발] retrofit으로 텍스트 받아오기. 본문
반응형
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");
}
});
반응형
'안드로이드 개발 > 안드로이드' 카테고리의 다른 글
[안드로이드UI] 채팅 대화내역 키보드에 안가리게 하기. (0) | 2023.04.01 |
---|---|
[안드로이드개발] 릴리즈 모드에서만 죽는 현상 proguard관련 (0) | 2023.03.31 |
[Android] 웹브라우저에서 텍스트 선택후 앱 열기 (0) | 2023.02.13 |
[Android] 텍스트 꾸미기 (Span) (0) | 2022.12.29 |
[Android] SPAN_EXCLUSIVE_INCLUSIVE (0) | 2022.12.29 |