【Android基础】HttpURLConnection

it2023-03-01  92

目录

一、什么是HTTP二、GET 和 POST1、GET2、POST 三、GET 使用步骤1、创建一个URL对象2、获取HttpURLConnection对象实例3、自由定制4、获取响应码5、获得服务器返回的输入流6、将HTTP连接关掉 四、POST 使用步骤五、下载图片并显示1、源码2、效果图

一、什么是HTTP

Hyper Text Transfer Protocol(超文本传输协议)(HTTP)的设计目的是保证客户机与服务器之间的通信。 HTTP 的工作方式是客户机与服务器之间的请求-响应协议。 举例:客户端(浏览器)向服务器提交 HTTP 请求,服务器向客户端返回响应。

二、GET 和 POST

Http协议支持的操作有GET、POST、HEAD、PUT、TRACE、OPTIONS、DELETE,其中最常用的是GET和POST。

1、GET

GET 请求可被缓存GET 请求保留在浏览器历史记录中GET 请求可被收藏为书签GET 请求有长度限制GET 请求发送的键值对随着URL一起发送,一旦该URL被黑客截获,那么就能看到发送的键值对信息,所以不应在处理敏感数据时使用GET 请求只应当用于获取数据

2、POST

POST 请求不会被缓存POST 请求不会保留在浏览器历史记录中POST 不能被收藏为书签POST 请求对数据长度没有要求POST 请求的URL中追加键值对参数,不过这些键值对参数不是随着URL发送的,而是被放入到请求体中发送,安全性高些。

三、GET 使用步骤

1、创建一个URL对象

URL url = new URL("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2239146502,165013516&fm=27&gp=0.jpg");

2、获取HttpURLConnection对象实例

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

3、自由定制

//设置请求方式 connection.setRequestMethod("GET"); //设置连接超时毫秒数 connection.setConnectTimeout(8000); //设置读取超时毫秒数 connection.setReadTimeout(8000);

4、获取响应码

int responseCode = connection.getResponseCode();

5、获得服务器返回的输入流

InputStream is = connection.getInputStream();

6、将HTTP连接关掉

connection.disconnect();

四、POST 使用步骤

有GET自然有POST 通过openConnection获取到的HttpURLConnection默认请求方式为Get,因此需要设置请求方式; POST方法不能缓存,需要手动设置为false。

connection.setRequestMethod("POST"); connection.setUseCaches(false);

每条数据都要以键值对的形式存在。数据与数据之间用“&”符号隔开。比如想要向服务器提交用户名和密码,就可以这样写:

OutputStream os = connection.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeBytes("username=admin&password=123456");

五、下载图片并显示

1、源码

public class MainActivity extends AppCompatActivity { private Button sendBT; private static ImageView responseview; private static Bitmap bitmap; private InputStream is; private HttpURLConnection connection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendBT = findViewById(R.id.send_request_bt); responseview = findViewById(R.id.response_view); WeakReference<MainActivity> mWeakReference = new WeakReference<MainActivity>(MainActivity.this); final MyHandler handler = new MyHandler(mWeakReference); sendBT.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { try { URL url = new URL("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2239146502,165013516&fm=27&gp=0.jpg"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { //拿到输入流 is = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(is); Message message = Message.obtain(); handler.sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } }).start(); } }); } private static class MyHandler extends Handler { WeakReference<MainActivity> mWeakReference; public MyHandler(WeakReference<MainActivity> mWeakReference) { this.mWeakReference = mWeakReference; } @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); MainActivity mainActivity = mWeakReference.get(); if (null != mainActivity){ responseview.setImageBitmap(bitmap); } } } @Override protected void onDestroy() { super.onDestroy(); connection.disconnect(); } }

2、效果图

最新回复(0)