@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new AsyncTask<String, Void, String>() { @Override protected String doInBackground(String... params) { try { URL url=new URL(params[0]); URLConnection connection=url.openConnection(); InputStream iStream=connection.getInputStream(); InputStreamReader isr=new InputStreamReader(iStream,"UTF-8"); BufferedReader br=new BufferedReader(isr); String line; while ((line=br.readLine())!=null) { Log.i("get", line); } br.close(); isr.close(); iStream.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }.execute("http://fanyi.youdao.com/openapi.do?keyfrom=aiHttpGet&key=1989341286&type=data&doctype=json&version=1.1&q=good"); } }); }
API:http://fanyi.youdao.com/openapi
时间: 2024-10-07 04:58:57