###第一次写,就当笔记了,哈哈,希望能帮助到别人,那就更好了
###数据源来自 有道API
MainActivity 代码
布局很简单就不加了。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText edit;
private Button search;
private TextView text;
private String url;
private String preresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edit);
search=(Button)findViewById(R.id.search);
text=(TextView)findViewById(R.id.text);
search.setOnClickListener(this);
}
private void print() throws JSONException {
JSONArray jsonArray = new JSONArray("[" + preresult + "]");
String message = null;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject != null) {
String errorCode = jsonObject.getString("errorCode");
if (errorCode.equals("20")) {
Toast.makeText(getApplicationContext(), "要翻译的文本过长",
Toast.LENGTH_SHORT);
} else if (errorCode.equals("30 ")) {
Toast.makeText(getApplicationContext(), "无法进行有效的翻译",
Toast.LENGTH_SHORT);
} else if (errorCode.equals("40")) {
Toast.makeText(getApplicationContext(), "不支持的语言类型",
Toast.LENGTH_SHORT);
} else if (errorCode.equals("50")) {
Toast.makeText(getApplicationContext(), "无效的key",
Toast.LENGTH_SHORT);
} else {
// 要翻译的内容
String query = jsonObject.getString("query");
message = query;
// 翻译内容
String translation = jsonObject
.getString("translation");
message += "\t" + translation;
// 有道词典-基本词典
if (jsonObject.has("basic")) {
JSONObject basic = jsonObject
.getJSONObject("basic");
if (basic.has("phonetic")) {
String phonetic = basic.getString("phonetic");
message += "\n\t" + phonetic;
}
if (basic.has("explains")) {
String explains = basic.getString("explains");
message += "\n\t" + explains;
}
}
// 有道词典-网络释义
if (jsonObject.has("web")) {
String web = jsonObject.getString("web");
JSONArray webString = new JSONArray("[" + web + "]");
message += "\n网络释义:";
JSONArray webArray = webString.getJSONArray(0);
int count = 0;
while (!webArray.isNull(count)) {
if (webArray.getJSONObject(count).has("key")) {
String key = webArray.getJSONObject(count)
.getString("key");
message += "\n\t<" + (count + 1) + ">"
+ key;
}
if (webArray.getJSONObject(count).has("value")) {
String value = webArray
.getJSONObject(count).getString(
"value");
message += "\n\t " + value;
}
count++;
}
}
}
}
}
text.setText(message);
}
private void asynchttpGet(String url) {
AsyncHttpClient client = new AsyncHttpClient();
String url0="http://fanyi.youdao.com/openapi.do?keyfrom=abcabc&key=1424388928&type=data&doctype=json&version=1.1&q=";
String url2=url0+url;
client.get(url2,new AsyncHttpResponseHandler()
{
@Override
public void onSuccess(String s) {
super.onSuccess(s);
preresult = s;
//text.setText(s);
System.out.println(s);
try {
print();
}catch (JSONException e)
{
System.out.println("++++++++++++++++++++++++++++++++++++++++++");
}
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Throwable throwable) {
super.onFailure(throwable);
Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onClick(View v) {
if(v.equals(search))
{
//Toast.makeText(MainActivity.this, "111111", Toast.LENGTH_SHORT).show();
url=edit.getText().toString().trim();
asynchttpGet(url);
}
}
}