zoukankan      html  css  js  c++  java
  • HttpClient

    package com.pingyijinren.test;
    
    import android.content.Intent;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class MainActivity extends AppCompatActivity{
        private Button button;
        private TextView textView;
        private static final int SHOW_RESPONSE=0;
        private Handler handler=new Handler(){
            public void handleMessage(Message message){
                switch(message.what){
                    case SHOW_RESPONSE:
                        String response=(String)message.obj;
                        textView.setText(response);
                }
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button=(Button)findViewById(R.id.button);
            textView=(TextView)findViewById(R.id.textView);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendRequestWithHttpURLConnection();
                }
            });
        }
    
        private void sendRequestWithHttpURLConnection(){
            new Thread(new Runnable(){
                @Override
                public void run(){
                    try{
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet=new HttpGet("https://www.baidu.com");
                        HttpResponse httpResponse=httpClient.execute(httpGet);
                        if(httpResponse.getStatusLine().getStatusCode()==200){
                            HttpEntity httpEntity=httpResponse.getEntity();
                            String response= EntityUtils.toString(httpEntity,"utf-8");
    
                            Message message=new Message();
                            message.what=SHOW_RESPONSE;
                            message.obj=response;
                            handler.sendMessage(message);
                        }
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
    
    
                }
            }).start();
        }
    }
  • 相关阅读:
    12C 中,发生脑裂时,节点保留策略
    如何修改集群的公网信息(包括 VIP)
    从 ASH 找到消耗 PGA 和 临时表空间 较多的 Top SQL_ID
    Oracle SCN详解
    10046 trace
    使用trace文件定位ORA-00060问题
    (转)计算机漏洞安全相关的概念POC 、EXP 、VUL 、CVE 、0DAY
    PowerShell 相关常用命令(update...)
    (转)主从同步常遇见问题处理-线上MYSQL同步报错故障处理总结
    pentestbox 安装后的基本设置
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5507937.html
Copyright © 2011-2022 走看看