Android xutils上传文件

35 篇文章 0 订阅
1 篇文章 0 订阅

依赖

    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();
            }

        });
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王鑫林呀!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值