zoukankan      html  css  js  c++  java
  • 每日总结

    1.HttpClient的学习

    HttpClient使用流程:

    ①创建HttpClient对象:HttpClient client = new DefaultHttpClient0;

    ②发送Get请求的话,创建HttpGet对象;发送Post请求的话创建HttpPost对象
    ③设置请求参数:两者都可以用setParams(HttpParams);Post还可用setEnity(HttpEntity)方法

    ④调用httpClient对象的execute)方法发送请求,该方法会返回一个HttpResponse
    ⑤对返回的HttpResponse调用:getAllHeaders().getHeaders(name)等方法可以获得服务器的响应头调用getEntity0方法可以取得HttpEntity对象,改对象包装了服务器的响应内容

    使用HttpClient发送GET请求:

    public class MainActivity extends Activity implements OnClickListener {
    
        private Button btnGet;
        private WebView wView;
        public static final int SHOW_DATA = 0X123;
        private String detail = "";
    
        private Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                if(msg.what == SHOW_DATA)
                {
                    wView.loadDataWithBaseURL("",detail, "text/html","UTF-8","");
                }
            };
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            setView();
        }
    
        private void initView() {
            btnGet = (Button) findViewById(R.id.btnGet);
            wView = (WebView) findViewById(R.id.wView);
        }
    
        private void setView() {
            btnGet.setOnClickListener(this);
            wView.getSettings().setDomStorageEnabled(true);
        }
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btnGet) {
                GetByHttpClient();
            }
        }
        private void GetByHttpClient() {
            new Thread()
            {
                public void run() 
                {
                        try {
                            HttpClient httpClient = new DefaultHttpClient();
                            HttpGet httpGet = new HttpGet("http://www.w3cschool.cc/python/python-tutorial.html");
                            HttpResponse httpResponse = httpClient.execute(httpGet);
                            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                                HttpEntity entity = httpResponse.getEntity();
                                detail = EntityUtils.toString(entity, "utf-8");
                                handler.sendEmptyMessage(SHOW_DATA);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                };
            }.start();
        }
    
    }
  • 相关阅读:
    HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
    php中0," ",null和false的区别
    php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项。
    php 解决上传中文文件名时出现乱码的问题
    php学习之有用的资源 总结
    php 问题及原因总结
    php 汉字验证码
    php 图片验证码
    php 字母和数字验证码
    php 使用imagettftext()函数出问题的原因
  • 原文地址:https://www.cnblogs.com/chenghaixiang/p/14912233.html
Copyright © 2011-2022 走看看