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();
        }
    
    }
  • 相关阅读:
    Tencent 闲聊对话机器人接口调用,画像:设计员小白
    logging模块简介python
    jieba分词的几种形式
    h5py这个坑-PyCharm Process finished with exit code -1073741819 (0xC0000005)
    python之six模块的用法six.py2 six.py3
    Swoole从入门到入土(8)——协程初探
    Swoole从入门到入土(7)——TCP服务器[大杂烩]
    Swoole从入门到入土(6)——TCP服务器[粘包]
    Swoole从入门到入土(5)——TCP服务器[异步任务]
    Swoole从入门到入土(4)——TCP服务器[正确重启]
  • 原文地址:https://www.cnblogs.com/chenghaixiang/p/14912233.html
Copyright © 2011-2022 走看看