看完慕课网的视频,觉得老师做的有些复杂,于是就自己写了一个精简版的。
使用到的技术:
有道翻译API:http://fanyi.youdao.com/openapi
Rxvolley网络框架:http://rxvolley.mydoc.io/
首先申请接口,打开,有道翻译API首页:
点击我是开发者:
填写相应的信息,拿到请求的数据:
在这里,大家可以试试这个接口
请求结果:
{"translation":["你好"],"basic":{"us-phonetic":"hɛˈlo, hə-","phonetic":"hə'ləʊ; he-","uk-phonetic":"hə'ləʊ; he-","explains":["n. 表示问候, 惊奇或唤起注意时的用语","int. 喂;哈罗","n. (Hello)人名;(法)埃洛"]},"query":"hello","errorCode":0,"web":[{"value":["你好","您好","hello"],"key":"Hello"},{"value":["凯蒂猫","昵称","匿称"],"key":"Hello Kitty"},{"value":["哈乐哈乐","乐扣乐扣"],"key":"Hello Bebe"}]}
去http://www.json.cn/进行解析
{
"translation":[
"你好"
],
"basic":{
"us-phonetic":"hɛˈlo, hə-",
"phonetic":"hə'ləʊ; he-",
"uk-phonetic":"hə'ləʊ; he-",
"explains":[
"n. 表示问候, 惊奇或唤起注意时的用语",
"int. 喂;哈罗",
"n. (Hello)人名;(法)埃洛"
]
},
"query":"hello",
"errorCode":0,
"web":[
{
"value":[
"你好",
"您好",
"hello"
],
"key":"Hello"
},
{
"value":[
"凯蒂猫",
"昵称",
"匿称"
],
"key":"Hello Kitty"
},
{
"value":[
"哈乐哈乐",
"乐扣乐扣"
],
"key":"Hello Bebe"
}
]
}
准备工作做好了,开始写逻辑;
首先,我们要使用Rxvolley网络框架:http://rxvolley.mydoc.io/
(详细可以看看)
使用RxVolley,需要在你的build.gradle文件中加入
compile ‘com.kymjs.rxvolley:rxvolley:1.1.4’
一个界面,一个activity
界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_background"
>
<TextView
android:id="@+id/mean"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="12dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center">
<EditText
android:id="@+id/in"
android:layout_width="300dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/sure"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/fanyi"/>
</LinearLayout>
</LinearLayout>
逻辑
public class MainActivity extends AppCompatActivity {
private TextView mean;
private EditText in;
private Button sure;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
private void initView() {
mean=(TextView)findViewById(R.id.mean);
in=(EditText)findViewById(R.id.in);
sure=(Button)findViewById(R.id.sure);
//为Button添加点击事件
sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
1、获取输入框的内容
2、判断输入是否为空
3、拼接api
4、向服务器发送请求
5、解析数据
6、在顶部显示
*/
//1、获取输入框的内容
String s=in.getText().toString();
//判断输入是否为空
if(s==null){
Toast.makeText(MainActivity.this,"输入框不能为空",Toast.LENGTH_LONG).show();
}
//输入为空
//3.拼接url
String a= "http://fanyi.youdao.com/openapi.do?keyfrom=aaa123ddd&key=336378893&type=data&doctype=json&version=1.1&q="+s;
//4.向服务器发送请求,使用rxvolley网络框架
//get请求简洁版实现
RxVolley.get(a, new HttpCallback() {
@Override
public void onSuccess(String t) {
Loger.debug("请求到的数据:" + t);
eJson(t);
}
});
}
}
);
}
//解析数据并显示
private void eJson(String json){
try{
JSONObject jsonObject = new JSONObject(json);
JSONObject object = jsonObject.getJSONObject("basic");
String s="美式发音"+ object.getString("us-phonetic")+"\n"+
"英式发音"+object.getString("uk-phonetic")+"\n"+
"释义:"+"\n"+object.getString("explains")+"\n"+"网络释义:"+"\n";
JSONArray ja = jsonObject.getJSONArray("web");
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject3 = (JSONObject) ja.get(i);
s=s+jsonObject3.getString("value")+"\n";
s=s+jsonObject3.getString("key")+"\n";
}
//在顶部显示
mean.setText(s);
}
catch (Exception e){
e.printStackTrace();
}
}
}
这样就完成了,如果有问题可以在下面评论交流哦