-
HTTP协议
-
- 版权声明:本文为博主原创文章,未经博主允许不得转载。https://mp.csdn.net/postedit/81838665
- 网络框架
Android自身已封装两种框架:
HttpClient(android 6.0,api23之后已废除)
HttpURLConnection
- 请求方式
常用的get/post方式。
Get请求:获取数据,传入的参数附在URL后面(显示在用户界面URL中,不放入HTTP请求IO流)。
Post请求:获取数据,传入请求体(最后放在HTTP请求IO流)较Get更安全,其他人URL无法查看你传入的参数
- 异步
任务顺序执行是同步,有明确时间先后顺序;
任务同时执行是异步,无明确的时间先后顺序。
Android主线程就是UI线程,主线程是同步执行。网络请求比较耗时,要放在子线程中去实现。实现完成后通知子线程更新UI即可。(异步)
postBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//第一种方法用子线程Thread
// new Thread(new Runnable() {
// @Override
// public void run() {
//调用要做的方法
// tupian()
// }
// }).start();
//调用的第二种方法
String parameter=new JSONObject().toString();
new B().execute("http://10.32.9.10:8080/basic/post/test",parameter);
}
});
//图片
private void tupian(){
try {
String[] images =new String[]{
"boy01.jpg","boy02.jpg" ,"boy03.jpg","boy04.jpg","girl01.jpg","girl04.jpg","girl02.jpg","girl03.jpg",
"animal01.jpg", "animal02.jpg", "animal03.jpg"
};
//random函数表示随机取0-1之间的一个小数
int random = (int) (Math.random()*11);
URL url = new URL("http://10.32.9.10:8080/imgs/"+images[random]);
//连接服务器
URLConnection connection = url.openConnection();
//强转为HTTP类型的URLConnection
HttpURLConnection httpURLConnection= (HttpURLConnection) connection;
//设置请求方式
httpURLConnection.setRequestMethod("GET");
if (200==httpURLConnection.getResponseCode()){
//服务器返回给我们的IO数据流
InputStream inputStream=httpURLConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//子线城发送massges到hander
Message message = handler.obtainMessage();
message.what=5;
message.obj=bitmap;
handler.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
- 关键代码
Get请求:
//设置服务器项目连接地址
URL url = new URL("http://localhost:8080/xxxxxxx");
//连接服务器
URLConnection connection = url.openConnection();
//强转为HTTP类型的URLConnection
HttpURLConnection httpURLConnection= (HttpURLConnection) connection;
//设置请求方式
httpURLConnection.setRequestMethod("GET");
//验证是否请求成功。200代表成功,404代表服务器项目资源找不到
if (200==httpURLConnection.getResponseCode()){
//服务器返回给我们的IO数据流
InputStream inputStream=httpURLConnection.getInputStream();
//将服务器返回给我们的IO流数据解析存放到字节输出流,最后转化为我们需要的字符串即可
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
//循环读取IO流数据,每次读取1kb数据
int length;
byte[] array=new byte[1024];
while ((length=inputStream.read(array))!=-1){
outputStream.write(array,0,length);
outputStream.flush();
}
outputStream.close();
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
Post请求:
//设置服务器项目连接地址
URL url = new URL("http://localhost:8080/xxxxxxx");
//连接服务器
URLConnection connection = url.openConnection();
//强转为HTTP类型的URLConnection
HttpURLConnection httpURLConnection= (HttpURLConnection) connection;
//设置请求方式
httpURLConnection.setRequestMethod("POST");
//设置接收请求体及输出响应体
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
//传入请求体
OutputStream outwritestream = conn.getOutputStream();
outwritestream.write(Json.getBytes());
outwritestream.flush();
outwritestream.close();//一定要close
//验证是否请求成功。200代表成功,404代表服务器项目资源找不到
if (200==httpURLConnection.getResponseCode()){
//服务器返回给我们的IO数据流
InputStream inputStream=httpURLConnection.getInputStream();
//将服务器返回给我们的IO流数据解析存放到字节输出流,最后转化为我们需要的字符串即可
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
//循环读取IO流数据,每次读取1kb数据
int length;
byte[] array=new byte[1024];
while ((length=inputStream.read(array))!=-1){
outputStream.write(array,0,length);
outputStream.flush();
}
outputStream.close();
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
//GET请求 用AsyncTask线程实现
private class A extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String result=null;
try {
URL url = new URL(strings[0]);
//连接服务器
URLConnection connection = url.openConnection();
//强转为HTTP类型的URLConnection
HttpURLConnection httpURLConnection= (HttpURLConnection) connection;
//设置请求方式
httpURLConnection.setRequestMethod("GET");
if (200==httpURLConnection.getResponseCode()){
//服务器返回给我们的IO数据流
InputStream inputStream=httpURLConnection.getInputStream();
//将服务器返回给我们的IO流数据解析存放到字节输出流,最后转化为我们需要的字符串即可
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
//循环读取IO流数据,每次读取1kb(最大)数据
int length;
byte[] array=new byte[1024];
while ((length=inputStream.read(array))!=-1){
outputStream.write(array,0,length);
outputStream.flush();
}
outputStream.close();
inputStream.close();
result=new String(outputStream.toByteArray(),"utf-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
wenben.setText(s);
}
}
-
- Handler
子线程不可直接与主线程交互。
作用:子线程与主线程交互的桥梁,它是不受线程约束的。
机制:
子线程1 |
子线程2 |
子线程3 |
handler |
主线程 |
① |
② |
3 |
- 子线程执行任务完成之后,把执行完的信息/数据通知给handler。消息封装是用Message实现,包括2部分
What:消息标识符
Obj:消息具体内容
Message message=handler.obtainMessage();
message.what=1;
message.obj=result;
handler.sendMessage(message);
- handler通知主线程做出处理。如显示数据,弹出框等
switch (msg.what){
case 1:
//得到Messges中的内容,转换为String
String result = (String)msg.obj;
textView.setText(result);
//得到Messges中的内容,转换为Bitmap(图片)
Bitmap bitmap = (Bitmap)msg.obj;
ImagesView.setImageBitmap(bitmap);
break;
case 2:
break;
case 3:
break;
}
③Looper:运行在子线程里面,循环检测是否有新任务执行成功。如果成功之后,加入消息队列里面。后续会推送给handler
面试题:简单说明Message,Looper,Handler运行机制。
-
- 测试接口
- Get请求
- 测试接口
http://服务器IP:8080/basic/get/test
http://服务器IP:8080/basic/get/userinfo?name=刘鹏展&sex=男
-
-
- Post请求
-
http://服务器IP:8080/basic/post/test
请求体:{}
http://服务器IP:8080/basic/post/userinfo
请求体:{“name”:”lpz”,”sex”:”男”}