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

  • 相关阅读:
    题解:[HNOI2004]树的计数
    题解:砝码称重
    题解:子矩阵(NOIP2014普及组T4)
    题解:低价购买
    题解:倍增三连击orz
    题解:UVA10140 Prime Distance
    题解:[JSOI2007]建筑抢修
    模板:三分法
    Databinging数据绑定
    开始
  • 原文地址:https://www.cnblogs.com/stareblankly/p/4974169.html
Copyright © 2011-2022 走看看