zoukankan      html  css  js  c++  java
  • Android 通过HTTP POST请求互联网数据

    @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

  • 相关阅读:
    Java NIO(六)选择器
    Java NIO(五)套接字通道
    Java NIO(四)文件通道
    Java NIO(三)通道
    Java NIO(二)缓冲区
    Java NIO(一)概述
    gcc中的内嵌汇编语言(Intel i386平台)
    一些汇编指令
    403 Forbidden解决方案
    Linux从入门到放弃
  • 原文地址:https://www.cnblogs.com/stareblankly/p/4974169.html
Copyright © 2011-2022 走看看