标签:
@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]); HttpURLConnection connection=(HttpURLConnection)url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); OutputStreamWriter osw=new OutputStreamWriter(connection.getOutputStream(),"UTF-8"); BufferedWriter bw=new BufferedWriter(osw); bw.write("keyfrom=aiHttpGet&key=1989341286&type=data&doctype=json&version=1.1&q=post"); bw.flush(); 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"); } }); }
API:http://fanyi.youdao.com/openapi
标签:
原文地址:http://www.cnblogs.com/stareblankly/p/4974169.html