@Override public T convert(ResponseBody value) throws IOException { String returned = value.string(); try { return gson.fromJson(returned, type); } catch (JsonParseException e) { return (T) returned; } }
/* * (non-Javadoc) * * @see okhttp3.ws.WebSocketListener#onMessage(okhttp3.ResponseBody) */ @Override public void onMessage(ResponseBody response) throws IOException { String message = response.string(); if (message == null) return; try { JsonObject json = new JsonParser().parse(message).getAsJsonObject(); if (json.has(ERROR)) { callback.onError(new RuntimeException(json.get(ERROR).getAsString())); } else if (json.has(RESULTS)) { callback.onTranscription(GSON.fromJson(message, SpeechResults.class)); } else if (json.has(STATE)) { if (!audioSent) { sendInputSteam(stream); socket.sendMessage(RequestBody.create(WebSocket.TEXT, buildStopMessage().toString())); audioSent = true; } else { socket.close(CLOSE_NORMAL, "Transcription completed"); } } } catch (JsonParseException e) { throw new RuntimeException("Error parsing the incoming message: " + response.string()); } }