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();
        }
    
    }
  • 相关阅读:
    语音识别六十年
    神经网络架构PYTORCH-几个概念
    Ubuntu 16.04 系统无法挂载u盘的问题
    技术的止境
    神经网络架构PYTORCH-宏观分析
    Python中parameters与argument区别
    神经网络架构PYTORCH-初相识(3W)
    【ES】学习9-聚合2
    【ES】学习8-聚合1
    【python】中文提取,判断,分词
  • 原文地址:https://www.cnblogs.com/chenghaixiang/p/14912233.html
Copyright © 2011-2022 走看看