Android使用百度翻译api

一直想试用一下百度翻译的接口,前些天从网上看了一篇关于json的博客,突然想试一下百度翻译api,于是我做了一个使用百度翻译api的Android小程序。

首先要使用百度翻译(也可以使用有道翻译)必须要有一个userkey,至于如何去申请userkey,还有请求的规则什么的,这里不再介绍,网站上写的都非常清楚。废话不多说下面直接说开发过程:

1、首先做一个界面出来(至于界面如何做,自己随意就好):edittext用于接收要翻译的内容,button用于发送请求,textview用于显示翻译好的内容。

Android使用百度翻译api_第1张图片

 

2、由于是连接百度翻译接口,所以联网是必须的,这里最好写一个检查网络的类,我这里简单写一个出来:

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、onclick方法,拼接请求字符串,(对字符串的处理,我建议还是自己写一下比较好点,我这里只是为了试验一下,所以写的很简单。。。)

Android使用百度翻译api_第2张图片

 

4、从网站上读取数据并显示:

private void fanYi(final Stringstr) {

 

                   newAsyncTask() {

                            @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、注意:

这个程序需要联网并读取网络连接情况所以就要添加两个权限:

一定要注意转换编码格式,不然会出现乱码。

 

                          ps:这个写的好的话,完全可以写一个翻译APP自己来使用啊。我是小白,写的不好的地方,大家指正啊。

你可能感兴趣的:(试验田)