(java function code) Baidu translation interface

java parsing json related

A json-lib.jar development package uses the dependency package
                                json-lib.jar development package requires the following development packages: 
                               * jakarta commons-lang 2.4
                               * jakarta commons-beanutils 1.7.0
                               * jakarta commons-collections 3.2
                               * jakarta commons- logging 1.1.1
                               *ezmorph 1.0.6

Baidu translation interface example code:

package com.baidu.translate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.nio.Buffer;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class BaiduTranslate {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
         String body="All roads lead to Rome";
         String from="zh";
         String to="en";
         
         String result=getResult(body,from,to);
         System.out.println("{"+result);
         String content="{"+result;
         String json=getDate(content);
         System.out.println(json);
	}
	//Baidu platform (translation interface) related data
	public static String getResult(String body,String from ,String to){
		String result="";
		//splicing related parameters
		String params="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=cxE4PWzno4Zx13LAvHX7ND5j&q="+body+"&from="+from+"&to="+to;		
		 try {
			URL url = new URL(params);
			URLConnection connection = url.openConnection();  
			//Set the connection time (10*1000)
			connection.setConnectTimeout(10*1000);
		    //set output
			connection.setDoOutput(true);
			//set output
			connection.setDoInput(true);
            //set cache
			connection.setUseCaches(false);			
			//outputstream-----output stream
			InputStream inputstream=connection.getInputStream();
			//cache character stream
			BufferedReader buffer = new BufferedReader(new InputStreamReader(inputstream));
			//return related results
			StringBuilder builder=new StringBuilder();
			while(buffer.read()!=-1){
				builder.append(buffer.readLine());				
			}
			//return related results
			result=builder.toString();
			//Cache character stream close operation
			buffer.close();

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		
		return result;
	}
	//Parse the relevant data information returned by the Baidu server platform
	public static String getDate(String result){
		String date="";
		
		JSONObject object=JSONObject.fromObject(result);
		JSONArray array=object.getJSONArray("trans_result");
		int length=array.size();
		for(int i=0;i<length;i++){
			JSONObject params=JSONObject.fromObject(array.get(i));
			String str=params.getString("dst");
			try {
				str=URLDecoder.decode(str,"utf-8");
				date = str;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}			
		}	
		return date;
		
	}
	

}


Results show:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389419&siteId=291194637