依赖
implementation 'org.xutils:xutils:3.5.0'
代码
public void voiceDatas(ViewHold viewHold, ChatMessage chatMessage) {
RequestParams params = new RequestParams(Urls);
params.setAsJsonContent(true);
List<KeyValue> list = new ArrayList<>();
//文件路径
File file1 = new File(chatMessage.getFilePath().toString().replace("file:///", "/"));
String extra = chatMessage.getMessage().getContent().getUserInfo().getExtra();
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(extra);
String sl = (String) jsonObject.get("sl");
list.add(new KeyValue("source_language", sl));
} catch (JSONException e) {
e.printStackTrace();
}
list.add(new KeyValue("file", file1));
//我的语言
list.add(new KeyValue("target", langue));
//如果有要用到这个代码的同学我自己入的参数你不用管将你自己的参数入到list里面就可以
//对方语言
MultipartBody body = new MultipartBody(list, "UTF-8");
SSLContext sslContext = MySSLContext.getSSLContext();
params.setSslSocketFactory(sslContext.getSocketFactory());
//设置超时时间
params.setConnectTimeout(1000 * 40);
params.setRequestBody(body);
x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
private ConnectivityManager cwjManager;
private String target_language;
@Override
public void onSuccess(String result) {
ChatMessage chatMessage1 = new Gson().fromJson(result, ChatMessage.class);
//判断是否是本机语言如果是就隐藏
String country = "" + CountryNameMap.getInstance().getCountryByCountry().get(chatMessage1.getTarget_language());
if (country.equalsIgnoreCase("null")) {
viewHold.tv_source.setVisibility(View.GONE);
} else {
viewHold.tv_source.setText(ParserJson.getValMap("translate"));
viewHold.tv_source.setVisibility(View.VISIBLE);
}
String target_text = chatMessage1.getTarget_text();
target_language = chatMessage1.getTarget_language();
Toast.makeText(context, target_language + "", Toast.LENGTH_SHORT).show();
viewHold.tvvoice_translate.setText(target_text);
DBMessage dbMessage = new DBMessage();
dbMessage.setMessageId(chatMessage.getMessage().getMessageId());
dbMessage.setVoiceTarget(chatMessage1.getTarget_text());
dbMessage.setVoicetranslate(chatMessage1.getOriginal_language());
dbMessage.save();
//点击查看原文与译文
viewHold.tv_source.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (flag) {
flag = false;
viewHold.tvvoice_translate.setText(chatMessage1.getOriginal_text());
String country = "" + CountryNameMap.getInstance().getCountryByCountry().get(chatMessage1.getTarget_language());
viewHold.tv_source.setVisibility(View.VISIBLE);
viewHold.tv_source.setText(ParserJson.getValMap("translatedFrom") + "" + country);
} else {
flag = true;
viewHold.tv_source.setVisibility(View.VISIBLE);
viewHold.tv_source.setText(ParserJson.getValMap("translate"));
viewHold.tvvoice_translate.setText(chatMessage1.getTarget_text());
}
}
});
}
@Override
public void onFinished() {
//上传完成
// viewHold.tv_translateclose.setVisibility(View.VISIBLE);
// viewHold.circle_translateclose.setVisibility(View.VISIBLE);
}
@Override
public void onCancelled(CancelledException cex) {
//取消上传
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
//上传失败
LogUtil.e("请求失败:" + ex.toString());
Log.e("ss", "aa" + ex.toString());
viewHold.tv_translateclose.setVisibility(View.GONE);
viewHold.circle_translateclose.setVisibility(View.GONE);
viewHold.im_translateclose.setVisibility(View.GONE);
viewHold.im_translate.setVisibility(View.VISIBLE);
viewHold.tv_source.setVisibility(View.GONE);
Toast.makeText(context, ParserJson.getValMap("no_language_detected_in_audio"), Toast.LENGTH_SHORT).show();
}
});
}