post方法

HttpURLConnection必须在子线程执行

/**                     * 方法名: new Thread                     * 作用: 开启新的子线程,                     * start(): 是准备运行子线程                     * run(): 运行子线程                     */                    new Thread(new Runnable() {                        @Override                        public void run() {                            String path = "https://网址";  //要提交的网址,如果是http需要android:usesCleartextTraffic="true"                            try {                                URL url = new URL(path);                                HttpURLConnection conn = (HttpURLConnection) url.openConnection();  //得到conn对象                                conn.setConnectTimeout(60000);  //连接服务器超时                                conn.setRequestMethod("POST");  //设置请求模式                                conn.setReadTimeout(60000);  //从服务器接收数据超时                                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 设置发送的数据为表单类型,会被添加到http body当中                                //conn.setRequestProperty ("Content-Type", "application/json");                                Gson gson = new Gson();                                Person person = new Person();                                person.setCha_xun("ceshi");                                person.setKai_shi_shi_jian(shu_zi);                                String data = gson.toJson(person); //将对象转换为JSON                                conn.setRequestProperty("Content-Length", String.valueOf(data.length()));//设置发送数据的长度                                conn.setDoOutput(true); //设置true表示连接为输出(提交),默认为false.                                conn.setUseCaches(false); //设置不用缓存                                conn.setDoInput(true);  //设置true表示连接为输入(提交),默认为true.                                conn.getOutputStream().write(data.getBytes()); //写入此连接的输出流,我理解为提交数据                                int responseCode = conn.getResponseCode(); //从HTTP响应消息获取状态代码                                if (responseCode == 200) {                                    InputStream inputStream = conn.getInputStream(); //从这个打开的连接读取数据的输入流(读取服务器返回的数据)                                    final String result = StreamUtils.readStream(inputStream); //解析数据                                    runOnUiThread(new Runnable() { //在子线程操作UI更新的方法                                        @Override                                        public void run() {                                            textView_cha_xun_fan_hui.setText(result);                                        }                                    });                                } else {                                    showToastInAnyThread("保存失败");                                }                            } catch (MalformedURLException e) {                                e.printStackTrace();                            } catch (IOException e) {                                e.printStackTrace();                            }                        }                    }).start();

StreamUtils类

public class StreamUtils {    public static String readStream(InputStream is){        try {            ByteArrayOutputStream baos = new ByteArrayOutputStream();            byte[] buffer = new byte[1024];            int len = 0;            while(( len = is.read(buffer))!=-1){                baos.write(buffer, 0, len);            }            is.close();            String result = baos.toString();            if(result.contains("gb2312")){                return baos.toString("gb2312");            }else{                return result;            }        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    public static Bitmap readBitmap(InputStream is){        return BitmapFactory.decodeStream(is);    }}

更多相关文章

  1. 基于TCP和多线程实现无线鼠标键盘-Socket(2)
  2. 打开系统wifi设置界面
  3. 关于Android如何改变ProgressBar进度条颜色
  4. EditText的属性
  5. Android中有关Handler的使用(三)
  6. android selector
  7. GridView的属性
  8. Android(安卓)Bundle类别
  9. mybatisplus的坑 insert标签insert into select无参数问题的解决

随机推荐

  1. Android 自动化测试—robotium(四)CheckBox
  2. 【Android开发学习07】存储简单数据的利
  3. Android手机开发:日期函数之判断某日期格
  4. JAVA使用AXMLPrinter获取APK中Androidman
  5. Android高手速成--第三部分 优秀项目
  6. Volley 源码解析
  7. Android 接入微信分享
  8. Android Display System Surface Flinger
  9. Android之封装支付宝支付
  10. flutter调用android原生组件