HTTP请求



HTTP请求

一.GET和POST方法有什么区别

get方式属于明文传参(不加密),在地址栏参数可见
post方式属于暗文传参,在地址栏参数不可见

二.HttpURLConnection请求网络数据实例

创建URL地址对象

URL url = new URL("https://www.baidu.com/");

创建Connection对象

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

创建输入流对象

InputStream inputStream = connection.getInputStream();

创建Reader对象

InputStreamReader reader = new InputStreamReader(inputStream);

创建BufferedReader对象

Bufferedder bufferedReader = new BufferedReader(reader); 

开始读取

String temp = "";
            StringBuffer stringBuffer = new StringBuffer();
            while ((temp = bufferedReader.readLine()) != null) {
                stringBuffer.append(temp);
            }
            Log.e("WEB", stringBuffer.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.myapplication.WebActivity">
<Button  android:layout_width="match_parent" android:layout_height="50dp" android:text="访问网络" android:id="@+id/web_btn"/>
    <Button  android:layout_width="match_parent" android:layout_height="50dp" android:text="访问网络图片" android:id="@+id/pic_btn"/>
LinearLayout>

主类:
按下按钮就能访问到创建url对象里面填的地址

package com.example.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class WebActivity extends AppCompatActivity {
    private Button web_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

            web_btn =findViewById(R.id.web_btn);
            web_btn.setOnClickListener(new View.OnClickListener()

            {
                @Override
                public void onClick (View view){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //getWebInfo();
                    }
               }).start();

            }
            });
      }

    public void getWebInfo() {
        try {
            //创建URL地址对象
            URL url = new URL("https://www.baidu.com/");
            //创建Connection对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //创建输入流对象
            InputStream inputStream = connection.getInputStream();
            //创建Reader对象
            InputStreamReader reader = new InputStreamReader(inputStream);
            //创建BufferedReader对象
            BufferedReader bufferedReader = new BufferedReader(reader);
            //开始读取
            String temp = "";
            StringBuffer stringBuffer = new StringBuffer();
            while ((temp = bufferedReader.readLine()) != null) {
                stringBuffer.append(temp);
            }
            Log.e("WEB", stringBuffer.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    }

三.HttpURLConnection加载网络图片实例

布局文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.myapplication.WebActivity">
    <Button  android:layout_width="match_parent" android:layout_height="50dp" android:text="访问网络图片" android:id="@+id/pic_btn"/>
    <ImageView  android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pic" android:scaleType="centerCrop"/>
LinearLayout>

主类:
创建一个内部类:

 public void getPic(){
        try {
            URL url = new URL("http://img2.imgtn.bdimg.com/it/u=2843597872,3901877888&fm=27&gp=0.jpg");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();
            Bitmap bm = BitmapFactory.decodeStream(inputStream);
            Message msg = handler.obtainMessage();
            msg.obj = bm;
            handler.sendMessage(msg);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

点击按钮就在下面显示从网络上面获取图片:

pic_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                            getPic();
                    }
                }).start();
            }
        });

因为传输数据不能在子线程里面进行,所以要用到handler:
这是传输数据

Message msg = handler.obtainMessage();
            msg.obj = bm;
            handler.sendMessage(msg);

接收数据:

private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bitmap bitmap = (Bitmap) msg.obj;
            pic.setImageBitmap(bitmap);
        }
    };

下面是完整代码:

package com.example.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class WebActivity extends AppCompatActivity {
    private Button pic_btn;
    private ImageView pic;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bitmap bitmap = (Bitmap) msg.obj;
            pic.setImageBitmap(bitmap);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        pic_btn = findViewById(R.id.pic_btn);
        pic = findViewById(R.id.pic);
        pic_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                            getPic();
                    }
                }).start();
            }
        });
      }
    public void getPic(){
        try {
            URL url = new URL("http://img2.imgtn.bdimg.com/it/u=2843597872,3901877888&fm=27&gp=0.jpg");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();
            Bitmap bm = BitmapFactory.decodeStream(inputStream);
            Message msg = handler.obtainMessage();
            msg.obj = bm;
            handler.sendMessage(msg);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    }

按下访问网络图片的效果图:
这里写图片描述

四.String StringBuffer StringBuilder有什么区别

String

字符串常量,String 是不可变的对象

StringBuffer

字符串变量(线程安全,多线程下操作大量的数据用StringBuffer)
StringBuffer 上的主要操作是 append 和 insert 方法,可重载这些方法,以接受任意类型的数据。

StringBuilder

字符串变量(非线程安全,单线程下操作大量的数据就使用StringBuilder)

  • 在执行速度上:StringBuilder–>StringBuffer–>String
  • String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象。StringBuffer 类则结果就不一样了,每次结果都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,再改变对象引用。