天天看点

第九章 网络编程HTTP简介

HTTP简介

HTTP(Hyper Text Transfer Protocol)协议即超文本传输协议,它规定了浏览器和万维网服务器之间互相通信的规则。

HTTP/1.1协议中共定义了八种方法来表明Request-URI指定的资源的不同操作方式。其中最常用的两种请求方式是GET和 POST。

GET方式与POST方式的区别

•GET方式以实体的方式得到由请求URL所指向的资源信息,向服务器提交

的参数跟在请求URL后面。使用GET方式访问网络URL的长度是有限制的,

请求URL的长度要小于1K。

•POST方式用来向目的服务器发出请求,要求它接收被附在请求后的实体。

它向服务器提交的参数在请求后的实体中,POST方式对URL的长度是没有限制的。

1.HttpURLConnection

网络请求要使用子线程;

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">https://www.baidu.com/</domain>
<pin-set expiration="2020-08-01">
<pin digest="SHA-256">7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=
</pin>
<!-- backup pin -->
<pin digest="SHA-256">fwza0LRMXouZHRC8Ei+4PyuldPDcf3UKgO/04cDM1oE=
</pin>
</pin-set>
</domain-config>
</network-security-config>
           
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
           
new Thread(new Runnable() {
@Override
public void run() {
try{
//1.构建请求地址
URL url=new URL("https://www.baidu.com/");
//2.获取HttpUriConnection对象
HttpURLConnection connection= (HttpURLConnection)
url.openConnection();
//3.设置请求信息
/** json 数据传递
* connection.setRequestMethod("GET");
* connection.setDoOutput(true);
* connection.setDoInput(true);
* connection.setUseCaches(false);
* String data= (unmae=admin) + (passs=123);要求数据格式
* OutputStream out=connection.getOutputStream();
* out.write(data);
*/
connection.setConnectTimeout(50000);
//connection.setRequestProperty("charset","utf-8");
//连接
connection.connect();
//4.获取响应
if(connection.getResponseCode()==200){
//5.处理响应内容
InputStream in=connection.getInputStream();
2.Json
1.格式:
2.解析
2.1 原生API JSONObject
2.2 Gson
InputStreamReader isr=new InputStreamReader(in,"utf-8");
BufferedReader reader=new BufferedReader(isr);
String line;
while((line=reader.readLine())!=null){
System.out.println(line);
}
reader.close();
isr.close();
in.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
           

2.Json

1.格式:

//单个json
{
"id":2, "name":"大虾",
"price":12.3,
"imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg"
}
           
//数组
[
{
"id":1, "name":"大虾1",
"price":12.3,
"imagePath":"http://192.168.10.165:8080/f1.jpg"
},
{
"id":2, "name":"大虾2",
"price":12.5,
"imagePath":"http://192.168.10.165:8080/f2.jpg"
}
]
           

2.解析

2.1 原生API JSONObject

JSONObject json=new JSONObject(str);
String obj=json.getString("weatherinfo");
JSONObject json2=new JSONObject(obj);
System.out.println(json2.getString("city"));
           

2.2 Gson 引入类库到工程:创建json对应的对象实体类

Gson gson=new Gson();
Weatherinfo w= gson.fromJson(str,Weatherinfo.class);
//类型
//对象数组
//List<Weatherinfo> list = gson.fromJson(str,new TypeToken<List<Weatherinfo>>
(){}.getType());
           

2.3 Fastjson

List<Weatherinfo> list =JSON.parseArray(str,Weatherinfo.class);
for(Weatherinfo w:list){
System.out.println("转换结果="+w);
}