Android uses Baidu translate api

I have always wanted to try out the interface of Baidu Translate. I read a blog about json from the Internet a few days ago, and suddenly wanted to try the Baidu Translate api, so I made an Android applet that uses the Baidu Translate api.

First of all, to use Baidu translation (you can also use Youdao translation), you must have a userkey. As for how to apply for a userkey, as well as the rules of the request, I will not introduce it here, but it is very clear on the website. Without further ado, let's talk about the development process directly:

1. First make an interface (as for how to do the interface, you can do whatever you want): edittext is used to receive the content to be translated, button is used to send requests, and textview is used to display the translated content.


2. Since it is connected to the Baidu translation interface, networking is necessary. It is best to write a class for checking the network here. I will simply write one here:

public class NetworkStatus {

 

         publicstatic boolean isNetworkAvailable(Activity activity) {

                   Contextcontext = activity.getApplicationContext();

                  

                   ConnectivityManagerconnectivityManager = (ConnectivityManager) context

                                     .getSystemService(Context.CONNECTIVITY_SERVICE);

 

                   if(connectivityManager == null) {

                            returnfalse;

                   }else {

                            NetworkInfo[]networkInfo = connectivityManager.getAllNetworkInfo();

 

                            if(networkInfo != null && networkInfo.length > 0) {

                                     for(int i = 0; i < networkInfo.length; i++) {

                                               if(networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {

                                                        returntrue;

                                               }

                                     }

                            }

                   }

                   returnfalse;

         }

}

3. The onclick method, splicing the request string, (for the processing of the string, I suggest that it is better to write it yourself, I am just here to test it, so it is very simple to write...)


4. Read data from the website and display:

private void fanYi(final Stringstr) {

 

                   newAsyncTask<String, Void, String>() {

                            @Override

                            protectedString doInBackground(String... params) {

                                     try{

                                               URLurl = new URL(params[0]);

                                               HttpURLConnectionconnection = (HttpURLConnection) url

                                                                 .openConnection();

                                               InputStreamis = connection.getInputStream();

                                               InputStreamReaderisr = new InputStreamReader(is, "utf-8");

                                               BufferedReaderbf = new BufferedReader(isr);

                                               Stringline;

                                               StringBuffersb = new StringBuffer();

                                               while((line = bf.readLine()) != null) {

                                                        System.out.println(line);

                                                        sb.append(line);

                                               }

                                               bf.close();

                                               isr.close();

                                               is.close();

                                               JSONObjectjsonObject = new JSONObject(sb.toString());

                                               JSONArraytrans_result = jsonObject

                                                                 .getJSONArray("trans_result");

                                               StringBufferafterText = new StringBuffer();

                                               for(int i = 0; i < trans_result.length(); i++) {

                                                        JSONObjectjo = trans_result.optJSONObject(i);

                                                        afterText.append(jo.getString("dst"));

                                               }

                                               textAfter= afterText.toString();

                                     }catch (Exception e) {

                                               e.printStackTrace ();

                                     }

                                     returnparams[0];

                            }

 

                            @Override

                            protectedvoid onPostExecute(String result) {

                                     //TODO Auto-generated method stub

                                     tvTextAfter.setText(textAfter);

//                                  super.onPostExecute(result);

                            }

                   }.execute(str);

                  

         }

 

5. Note:

This program needs to be connected to the Internet and read the network connection, so it is necessary to add two permissions:

<uses-permissionandroid:name="android.permission.INTERNET"/>

<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>

Be sure to pay attention to the conversion encoding format, otherwise there will be garbled characters.

 

                          ps: If this is well written, you can write a translation app and use it yourself. I'm a novice, please correct me if my writing is not good.

Guess you like

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