use of com.aimmatic.natural.voice.rest.response.LangResponse in project natural-voice-mobile-sdk-android by aimmatic.
the class Language method loadLanguage.
@VisibleForTesting
static void loadLanguage(final AppContext appContext, final Callback callback) {
OkHttpClient client = appContext.getOkHttpClient();
final Request request = new Request.Builder().url(appContext.getHost() + Resources.ApiVersion + Resources.NaturalVoiceLanguage).build();
final File dir = appContext.getDataDir();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
if (callback != null)
callback.onFailure(call, e);
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.code() == 200 && response.body() != null) {
ResponseBody responseBody = response.body();
BufferedSource source = responseBody.source();
// request the entire body.
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
String resp = buffer.clone().readString(Charset.forName("UTF-8"));
LangResponse lr = new Gson().fromJson(resp, LangResponse.class);
File file = new File(dir, "aimmatic-speech-lang.json");
FileOutputStream out = new FileOutputStream(file);
String data = new Gson().toJson(lr.getLanguages());
out.write(data.getBytes());
out.close();
}
if (callback != null)
callback.onResponse(call, response);
}
});
}
use of com.aimmatic.natural.voice.rest.response.LangResponse in project natural-voice-mobile-sdk-android by aimmatic.
the class GetLangTest method testGetLanguage.
@Test
public void testGetLanguage() throws InterruptedException, IOException {
final AsyncWait cdl = new AsyncWait(1);
TestAppContext appContext = new TestAppContext();
Language.loadLanguage(appContext, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
cdl.failed = true;
cdl.countDown();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code() == 200 && response.body() != null) {
String resp = response.body().string();
LangResponse lr = new Gson().fromJson(resp, LangResponse.class);
cdl.response = new Gson().toJson(lr.getLanguages());
cdl.failed = false;
} else {
cdl.failed = true;
}
cdl.countDown();
}
});
cdl.await();
Assert.assertFalse("Loading language failed", cdl.failed);
File file = new File(appContext.getDataDir(), "aimmatic-speech-lang.json");
Assert.assertTrue("File not existed", file.exists());
FileInputStream in = new FileInputStream(file);
byte[] bf = new byte[in.available()];
in.read(bf);
in.close();
Assert.assertTrue("Response is not identical", new String(bf).compareTo((String) cdl.response) == 0);
}
Aggregations