uniapp 原生android插件实现get和post请求

 前言

由于Android 4.0 之后不能在主线程中请求HTTP请求,所以请求必须放在子线程中进行。

Http请求方式Get与Post的简介 先来了解Http协议:Http(HyperText Transfer Protocol超文本传输协议)是一个设计来使客户端和服务器顺利进行通讯的协议。 HTTP在客户端和服务器之间以request-response protocol(请求-回复协议)工作。 简单来说呢,Get与Post就是基于http协议的网络数据交互方式。

实现

GET

源码

以下为uniapp原生插件中实现的get请求,android原生可以在去除uniapp相关后使用。

    @UniJSMethod(uiThread = true)
    public void httpGet (final String username, final String password, final UniJSCallback callback) {
        new Thread(new Runnable(){
            @Override
            public void run() {
            String path = "http://192.168.0.220:8000/api/android/http/test/get?username="+ username + "&password=" + password;

            try {
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");
                //获得结果码
                int responseCode = connection.getResponseCode();
                if(responseCode ==200){
                    //请求成功 获得返回的流
                    InputStream is = connection.getInputStream();
                    // 获取返回结果
                    int len = -1;
                    byte buf[] = new byte[128];
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    while ((len=is.read(buf))!=-1){
                        baos.write(buf,0,len);
                    }
                    String result = new String(baos.toByteArray());
                    JSONObject res = JSONObject.parseObject(result);

                    callback.invoke(res);
                }else {
                    callback.invoke(null);
                }
            } catch (IOException e) {
                e.printStackTrace();
                callback.invoke(e.getMessage());
            }
            }
        }).start();
    }

测试

get请求测试结果如下:

POST

源码

以下为uniapp原生插件中实现的post请求,android原生可以在去除uniapp相关后使用。

    @UniJSMethod(uiThread = true)
    public void httpPost (final String username, final String password, final UniJSCallback callback) {
        new Thread(new Runnable(){
            @Override
            public void run() {
            String path = "http://192.168.0.220:8000/api/android/http/test";
            try {
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("POST");

                String data = "username="+username+"&password="+password;
                byte[] data_byte = data.getBytes();

                connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

                connection.setDoOutput(true);
                OutputStream outputStream = connection.getOutputStream();
                outputStream.write(data_byte);

                int responseCode = connection.getResponseCode();
                if(responseCode ==200){
                    InputStream is = connection.getInputStream();
                    // 获取返回结果
                    int len = -1;
                    byte buf[] = new byte[128];
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    while ((len=is.read(buf))!=-1){
                        baos.write(buf,0,len);
                    }
                    String result = new String(baos.toByteArray());
                    JSONObject res = JSONObject.parseObject(result);

                    callback.invoke(res);
                }else {
                    callback.invoke(null);
                }
            } catch (IOException e) {
                e.printStackTrace();
                callback.invoke(e.getMessage());
            }
            }
        }).start();
    }

测试

post请求测试结果如下:

 参考

猜你喜欢

转载自blog.csdn.net/Douz_lungfish/article/details/126607738