程序猿全敏的gravatar头像
程序猿全敏 2016-01-19 10:25:10

java httpclient调用百度api接口实现翻译测试

import java.util.List;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
 
import com.google.gson.Gson;
 
/**
 * @author quanmin
 *
 */
public class Baidu {
 
    private static String url = " http://openapi.baidu.com/public/2.0/bmt/translate";
 
    private static String api_key = "SK5Xqu91Dd95RocfjaU2ndGn";
 
    public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        method.setQueryString(new NameValuePair[] {
                new NameValuePair("from", "zh"),
                new NameValuePair("to", "jp"),
                new NameValuePair("client_id", api_key),
                // 多条内容用\n分隔
                new NameValuePair("q", "初次见面") });
 
        client.executeMethod(method);
        String response = new String(method.getResponseBodyAsString());
        System.out.println(Native2AsciiUtils.ascii2Native(response));
        method.releaseConnection();
 
        Gson gson = new Gson();
        BaiduTrans bt = gson.fromJson(response, BaiduTrans.class);
        for (TransResult tr : bt.getTrans_result()) {
            System.out.println(tr.getDst());
        }
    }
 
    class BaiduTrans {
        private String from;
        private String to;
        private List<TransResult> trans_result;
 
        public String getFrom() {
            return from;
        }
 
        public void setFrom(String from) {
            this.from = from;
        }
 
        public String getTo() {
            return to;
        }
 
        public void setTo(String to) {
            this.to = to;
        }
 
        public List<TransResult> getTrans_result() {
            return trans_result;
        }
 
        public void setTrans_result(List<TransResult> trans_result) {
            this.trans_result = trans_result;
        }
    }
     
    class TransResult {
        public String getSrc() {
            return src;
        }
 
        public void setSrc(String src) {
            this.src = src;
        }
 
        public String getDst() {
            return dst;
        }
 
        public void setDst(String dst) {
            this.dst = dst;
        }
 
        private String src;
        private String dst;
    }
}

 

还有一个是Native和Ascii转换

java httpclient调用百度api接口实现翻译测试


打赏

文件名:trans.rar,文件大小:1017.672K 下载
  • /
      • /trans
        • /trans/.classpath
        • /trans/.mymetadata
        • /trans/.project
          • /trans/.settings
            • /trans/.settings/org.eclipse.jdt.core.prefs
          • /trans/WebRoot
              • /trans/WebRoot/META-INF
                • /trans/WebRoot/META-INF/MANIFEST.MF
              • /trans/WebRoot/WEB-INF
                  • /trans/WebRoot/WEB-INF/classes
                    • /trans/WebRoot/WEB-INF/classes/Baidu$BaiduTrans.class
                    • /trans/WebRoot/WEB-INF/classes/Baidu$TransResult.class
            • /trans/WebRoot/index.jsp
最代码最近下载分享源代码列表最近下载
abdkfksdkf  LV15 2023年3月2日
gao123qq  LV21 2021年5月7日
拾光979  LV11 2020年2月25日
lixiaominghahaha  LV10 2019年4月17日
idcomcn2003  LV5 2019年4月8日
cao1992  LV24 2019年1月21日
17608417105  LV9 2018年9月10日
2303801086  LV1 2018年7月9日
1324488732  LV27 2018年7月1日
lhdznb  LV16 2018年5月5日
最代码最近浏览分享源代码列表最近浏览
2206371875  LV7 2023年11月18日
1358849392  LV20 2023年10月10日
xianyu091012  LV2 2023年7月19日
2017143155  LV12 2023年6月27日
abdkfksdkf  LV15 2023年3月2日
12cq345  LV6 2023年2月24日
zdmlychee  LV2 2022年11月22日
sink122406  LV12 2022年7月11日
mq13947193109  LV19 2022年7月5日
wangdengzhe  LV7 2022年4月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友