为了账号安全,请及时绑定邮箱和手机立即绑定

android中post,put,get,delete简洁写法

标签:
Android
public class HttpUtil {

    private static final String TAG = HttpUtil.class.getSimpleName();

    public static String get(final String strUrl) {

        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection)
                    url.openConnection();
            conn.setReadTimeout(5000);
            conn.setRequestMethod("GET");

            int response = conn.getResponseCode();
            if(response == HttpURLConnection.HTTP_OK) {
                InputStream stream = conn.getInputStream();
                return dealResponseResult(stream);

            }else{
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String doDelete(String urlStr,String params){
        try{
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setReadTimeout(5000);
            conn.setRequestMethod("DELETE");

            //获得一个输出流,向服务器写入数据
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(params.getBytes());

            int response = conn.getResponseCode();
            if(response == HttpURLConnection.HTTP_OK) {
                InputStream stream = conn.getInputStream();
                return dealResponseResult(stream);

            }else{
                Log.d(TAG,"<<<<<response="+response);
                return null;
            }
        }catch (IOException ex) {
            ex.printStackTrace();
        }

        return null;
    }



    public static String post(final String strUrl, String params) {

        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setReadTimeout(5000);
            conn.setRequestMethod("POST");

            //设置请求体的类型是文本类型
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

            //设置请求体的长度
            conn.setRequestProperty("Content-Length",String.valueOf(params.getBytes().length));

            //获得一个输出流,向服务器写入数据
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(params.getBytes());

            int response = conn.getResponseCode();
            if(response == HttpURLConnection.HTTP_OK) {
                InputStream stream = conn.getInputStream();
                return dealResponseResult(stream);

            }else{
                Log.d(TAG,"<<<<<response="+response);
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String put(final String strUrl, String params) {
        try{
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setReadTimeout(5000);
            conn.setRequestMethod("PUT");

            //获得一个输出流,向服务器写入数据
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(params.getBytes());

            int response = conn.getResponseCode();
            if(response == HttpURLConnection.HTTP_OK) {
                InputStream stream = conn.getInputStream();
                return dealResponseResult(stream);

            }else{
                Log.d(TAG,"<<<<<response="+response);
                return null;
            }
        }catch (IOException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    private static String dealResponseResult(InputStream stream) throws IOException{
        StringBuffer buffer = new StringBuffer();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        return buffer.toString();
    }


}

Http的操作可以分以下几部:

对于get请求可以分以下几部:

  1. 创建URL

  2. 生成httpURLConnection

  3. 设置read timeout

  4. 设置那个request method

  5. 获取response code

  6. 获取服务器的inputStream


对于post,delete,put请求可以分以下几部:

  1. 创建URL

  2. 生成httpURLConnection

  3. 设置read timeout

  4. 设置那个request method

  5. 用outputStream往服务器上写数据

  6. 获取response code

  7. 获取服务器的inputStream


点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消