每日总结

今天上了软件构造课程。

百度机器翻译SDK实验

 

复制代码
package translate;

import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;

class Sample {
    public static final String API_KEY = "0r";
    public static final String SECRET_KEY = "hiL2";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static String getResulten(String ss) throws IOException{
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\"from\":\"zh\",\"to\":\"en\",\"q\":\""+ss+"\"}");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=" + getAccessToken())
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        //System.out.println(response.body().string());
        String jsonString = response.body().string();
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONObject result = jsonObject.getJSONObject("result");
        JSONArray transResult = result.getJSONArray("trans_result");
        JSONObject translate = transResult.getJSONObject(0);
        String src = translate.getString("src");
        String dst = translate.getString("dst");
        System.out.println("src: " + src);
        System.out.println("dst: " + dst);
        return dst;
    }

    public static String getResultzh(String ss) throws IOException{
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\"from\":\"en\",\"to\":\"zh\",\"q\":\""+ss+"\"}");
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=" + getAccessToken())
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        //System.out.println(response.body().string());
        String jsonString = response.body().string();
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONObject result = jsonObject.getJSONObject("result");
        JSONArray transResult = result.getJSONArray("trans_result");
        JSONObject translate = transResult.getJSONObject(0);
        String src = translate.getString("src");
        String dst = translate.getString("dst");
        System.out.println("src: " + src);
        System.out.println("dst: " + dst);
        return dst;
    }
    public static void main(String[] args) throws IOException {
        String ss=Sample.getResulten("你好");
        String sss=Sample.getResultzh("hello");
        System.out.println(ss);
        System.out.println(sss);
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     *
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
                + "&client_secret=" + SECRET_KEY);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        return new JSONObject(response.body().string()).getString("access_token");
    }

}
复制代码

 

posted @   一个小虎牙  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示