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

    private EditText et;
        private TextView tv;
        HttpClient client;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et=(EditText) findViewById(R.id.et);
            tv=(TextView) findViewById(R.id.tv);
            client=new DefaultHttpClient();
            findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    readNet("http://localhost:8080/get",et.getText()+"");
                    
                }
            });
        }
        
        
        public void readNet(String url,String name){
            new AsyncTask<String, Void, String>() {
    
                @Override
                protected String doInBackground(String... params) {
                    String urlString=params[0];
                    HttpPost post=new HttpPost(urlString);
                    try {
                        HttpResponse response=client.execute(post);
                        List<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();
                        list.add(new BasicNameValuePair("name", params[1]));
                        post.setEntity(new UrlEncodedFormEntity(list));
                        String v=EntityUtils.toString( response.getEntity());
                        return v;
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    return null;
                }
    
                @Override
                protected void onPostExecute(String result) {
                    tv.setText(result);
                }
                
            }.execute(url,name);
        }

    (ˉ(∞)ˉ) :由于是使用表单的方式提交数据,所以应该用

     List<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();
    键值对的方式。
  • 相关阅读:
    oracle中job定时调用存储过程的实例
    oracle recyclebin详解(闪回删除的表)
    启动和禁用约束及删除违反约束的记录
    儒轩画的老鼠
    SQLServer2005重建索引
    [转]你真的了解 console 吗
    [转]C# 理解lock
    [转]大话 程序猿 眼里的 高并发
    莆田系医院名单
    .Net WEB 程序员需要掌握的技能
  • 原文地址:https://www.cnblogs.com/stareblankly/p/4974499.html
Copyright © 2011-2022 走看看