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>();
    键值对的方式。
  • 相关阅读:
    PHP Context学习系列《十》
    学习php记录《九》
    学习php记录《八》
    php学习记录《七》
    换到新工作后
    学习php记录《六》
    学习php记录《五》
    学习php记录《四》
    学习php记录《三》
    html基础
  • 原文地址:https://www.cnblogs.com/stareblankly/p/4974499.html
Copyright © 2011-2022 走看看