zoukankan      html  css  js  c++  java
  • 使用Http协议访问网络

    1.设置一个布局用于显示得到的数据

    首先要new Thread()并且在里面new Runable()方法。首先获取HttpURLConnection实例。通过Url设置UI地址,通过uri调用openConnction()方法并且赋值于我们获取的实例,通过实例设置get请求。它是以的形式返回所以我们要定义一个Buff流来读取它。并且定义一个动态数组StringBuilder用来存储我们读取的流,最后将数组里的数据转为字符串用控件调用setText()方法显示在界面上

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        TextView mresponseText;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        mresponseText= (TextView) findViewById(R.id.text);
         Button send= (Button) findViewById(R.id.butn);
            send.setOnClickListener(this);
         
        }
    
        @Override
        public void onClick(View v) {
            if (v.getId()==R.id.butn){
                sendRequestUri();
            }
        }
    
        //发送url请求
        private void sendRequestUri() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpURLConnection connsent = null;
                    BufferedReader reader = null;
    
                    try {
                        URL uri = new URL("http://tuijian.hao123.com/");//设置uri
                        connsent = (HttpURLConnection) uri.openConnection();//打开所设定的uri
                        connsent.setRequestMethod("GET");//发送get请求
                      InputStream in= connsent.getInputStream();//获取返回的流
                        reader = new BufferedReader(new InputStreamReader(in));//利用buff读取返回的流
                        StringBuilder response = new StringBuilder();//将流转换为字符串
                        String line;
                        while ((line=reader.readLine())!=null){
                            response.append(line);
                        }
                        show(response.toString());
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        if (reader!=null){
                            try {
                                reader.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }if (connsent!=null){
                            connsent.disconnect();
                        }
                    }
                }
            }).start();
        }
        public void show(final String respon){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mresponseText.setText(respon);
                }
            });
    
        }
    }
    

     二。Okhttp方法

    首先要添加一个依赖

       compile 'com.squareup.okhttp3:okhttp:3.4.1'
    

     创建一个OkHttppClient实例

    new OkHttppClien();

    如果想要发送请求要创建一个Request对象

    new Request.Builder().uri("uri").build();

    接下来用我们定义的实例的newCall()方法调用execute()方法,用来发送和得到返回数据的。ewCall()中的参数为我们发送的ui数据

    最后将返回的数据转为字符串类型显示到控件上

    private void sendRequestUriOkhttp() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                 OkHttpClient client= new OkHttpClient();
                    //发送请求
                   Request request= new Request.Builder().url("http://tuijian.hao123.com/").build();
                        Response response= client.newCall(request).execute();//execute()发送请求并且得到返回值
                       String responseData= response.body().toString();//将返回值转变为数组
                        show(responseData);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
    
        }
    
  • 相关阅读:
    CDQ分治
    2-sat
    整体二分
    apache性能优化
    apache反向代理出现502调整
    hadoop学习笔记肆--元数据管理机制
    ssh 免密码登录配置,及其原理
    extjs 中的一些鲜为人知的属性(深渊巨坑)
    hadoop学习笔记叁--简单应用
    hadoop学习笔记贰 --HDFS及YARN的启动
  • 原文地址:https://www.cnblogs.com/lyl123/p/7194872.html
Copyright © 2011-2022 走看看