Java使用百度翻译接口开发app

到网站:百度翻译开放平台下载Java版本的demo

使用demo中的三个文件,main不用

默认返回的result内容是这样的:

bash 复制代码
 {"from":"en","to":"zh","trans_result":[{"src":"Sure! Here's a short paragraph in English: ","dst":"\u5f53\u7136\u8fd9\u91cc\u6709\u4e00\u5c0f\u6bb5\u82f1\u8bed\uff1a"},{"src":"\"The sun was setting behind the distant hills, casting a warm golden glow over the tranquil countryside. Birds chirped softly in the trees, and a gentle breeze rustled through the leaves. As evening descended, the sky turned shades of pink and orange, painting a breathtaking canvas above. It was a moment of serene beauty, a peaceful pause in the hustle and bustle of life.\"","dst":"\u201c\u592a\u9633\u843d\u5728\u8fdc\u5904\u7684\u5c71\u4e18\u540e\u9762\uff0c\u7ed9\u5b81\u9759\u7684\u4e61\u6751\u6295\u4e0b\u4e86\u6e29\u6696\u7684\u91d1\u8272\u5149\u8292\u3002\u9e1f\u513f\u5728\u6811\u4e0a\u8f7b\u8f7b\u5730\u9e23\u53eb\uff0c\u5fae\u98ce\u5728\u6811\u53f6\u4e2d\u6c99\u6c99\u4f5c\u54cd\u3002\u591c\u5e55\u964d\u4e34\uff0c\u5929\u7a7a\u53d8\u6210\u4e86\u7c89\u7ea2\u8272\u548c\u6a59\u8272\uff0c\u5728\u4e0a\u9762\u753b\u51fa\u4e86\u4e00\u5e45\u4ee4\u4eba\u60ca\u53f9\u7684\u753b\u5e03\u3002\u8fd9\u662f\u4e00\u4e2a\u5b81\u9759\u7684\u7f8e\u4e3d\u65f6\u523b\uff0c\u662f\u751f\u6d3b\u55a7\u56a3\u4e2d\u7684\u4e00\u4e2a\u5e73\u9759\u7684\u505c\u987f\u3002\u201d"}]}

使用下面的代码,会自动将结果提取出来:

java 复制代码
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class baidutranslation {

    private static final String APP_ID = "";
    private static final String SECURITY_KEY = "";

    public static void baiduTranslation(String ocrText, String src, String tgt, TranslationListener listener) {
        // 创建一个 AsyncTask 来执行网络请求
        TranslationTask task = new TranslationTask(ocrText, src, tgt, listener);
        task.execute();
    }

    private static class TranslationTask extends AsyncTask<Void, Void, String> {
        private String ocrText;
        private String src;
        private String tgt;
        private TranslationListener listener;

        public TranslationTask(String ocrText, String src, String tgt, TranslationListener listener) {
            this.ocrText = ocrText;
            this.src = src;
            this.tgt = tgt;
            this.listener = listener;
        }

        @Override
        protected String doInBackground(Void... voids) {
            // 在后台线程中执行网络请求
            TransApi api = new TransApi(APP_ID, SECURITY_KEY);
            return api.getTransResult(ocrText, src, tgt);
        }

        @Override
        protected void onPostExecute(String result) {
            // 在 UI 线程中处理网络请求的结果,并将提取出来的翻译文本传递给监听器
            if (listener != null) {
                try {
                    JSONObject jsonResult = new JSONObject(result);
                    JSONArray transResult = jsonResult.getJSONArray("trans_result");
                    StringBuilder translatedText = new StringBuilder();
                    for (int i = 0; i < transResult.length(); i++) {
                        JSONObject item = transResult.getJSONObject(i);
                        translatedText.append(item.getString("dst"));
                    }
                    listener.onTranslationResult(translatedText.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public interface TranslationListener {
        void onTranslationResult(String result);
    }
}

在其他函数中调用:

java 复制代码
// 执行翻译操作
        baidutranslation.baiduTranslation(ocrText, src, "zh", new baidutranslation.TranslationListener() {
            @Override
            public void onTranslationResult(String result) {
                // 处理翻译结果
                textView.setText(ocrText+"\n\n  翻译结果: \n\n " + result);
            }
        });
相关推荐
郑州光合科技余经理7 小时前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php
roman_日积跬步-终至千里7 小时前
【数据治理(6)】DataOps 不是调度工具,而是让数据产品长期可信的运行机制
java·服务器·网络
来了就未晚7 小时前
Python基础 学习代码存储与命名规范
开发语言·python·学习
落魄大学生之流水线上谋生计7 小时前
LangGraph 基本用法指南
java·windows·python·langchain
布莱克6057 小时前
数组详解:定义、作用、应用场景及与链表的区别(C/C++ 代码讲解)
c语言·开发语言·c++·数组
2601_962128417 小时前
SpringBoot篇(缓存层)
java·spring boot·缓存
hongmai6668888 小时前
MP2348GTL-Z:这颗24V/4A同步降压芯片,把性能和体积平衡得恰到好处
c语言·开发语言·arm开发·单片机·5g
多弗朗皮卡丘8 小时前
C语言梦开始的地方19:文件操作
c语言·开发语言
Dovis(誓平步青云)8 小时前
折叠屏悬停看视频,上半屏和下半屏应该各做什么
android·java·服务器·开发语言·安全·音视频
benchmark_cc8 小时前
Python量化实战:如何检测并剔除历史数据中的“闪崩/乌龙指”异常价格点?
开发语言·人工智能·python·量化·quantdash·量化数据源