Android HttpUrlConnection 发送网络请求步骤总结

来源:互联网 发布:js函数call 编辑:程序博客网 时间:2024/04/29 08:40

1,URL url = newURL(path);

2,通过url获取连接

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

3,设置请求方式:

conn.setRequestMethod(GET);

4,设置连接超时:

conn.setConnectTimeout(5000);

5,设置请求头的信息:

conn.setRequestProperty(User-Agent, Mozilla/5.0(compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0));

6,获取响应码:

intcode = conn.getResponseCode();

7,针对不同的响应码,做不同的操作:

请求码200,表明请求成功,获取返回内容的输入流:InputStream is = conn.getInputStream();

将输入流转换成字符串:

publicclass StreamTools {
    /**
     * 将输入流转换成字符串
     *
     * @param is
     *            从网络获取的输入流
     * @return
     */
    publicstatic String streamToString(InputStream is) {
        try{
            ByteArrayOutputStream baos = newByteArrayOutputStream();
            byte[] buffer = newbyte[1024];
            intlen = 0;
            while((len = is.read(buffer)) != -1) {
                baos.write(buffer,0, len);
            }
            baos.close();
            is.close();
            byte[] byteArray = baos.toByteArray();
            returnnew String(byteArray);
        }catch(Exception e) {
            Log.e(tag, e.toString());
            returnnull;
        }
    }
}

若返回值400,则是返回网络异常,做出响应的处理。 



1、

HttpUrlConnection发送POST请求

/**
     * 通过HttpUrlConnection发送POST请求
     *
     * @param username
     * @param password
     * @return
     */
    publicstatic String loginByPost(String username, String password) {
        String path = http://192.168.0.107:8080/WebTest/LoginServerlet;
        try{
            URL url = newURL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod(POST);
            conn.setRequestProperty(Content-Type, application/x-www-form-urlencoded);
            String data = username= + username + &password= + password;
            conn.setRequestProperty(Content-Length, data.length() + );
            // POST方式,其实就是浏览器把数据写给服务器
            conn.setDoOutput(true);// 设置可输出流
            OutputStream os = conn.getOutputStream(); // 获取输出流
            os.write(data.getBytes());// 将数据写给服务器
            intcode = conn.getResponseCode();
            if(code == 200) {
                InputStream is = conn.getInputStream();
                returnStreamTools.streamToString(is);
            }else{
                return网络访问失败;
            }
        }catch(Exception e) {
            e.printStackTrace();
            return网络访问失败;
        }
    }


HttpUrlConnection发送GET请求

/**
     * 通过HttpUrlConnection发送GET请求
     *
     * @param username
     * @param password
     * @return
     */
    publicstatic String loginByGet(String username, String password) {
        String path = http://192.168.0.107:8080/WebTest/LoginServerlet?username= + username + &password= + password;
        try{
            URL url = newURL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod(GET);
            intcode = conn.getResponseCode();
            if(code == 200) {
                InputStream is = conn.getInputStream(); // 字节流转换成字符串
                returnStreamTools.streamToString(is);
            }else{
                return网络访问失败;
            }
        }catch(Exception e) {
            e.printStackTrace();
            return网络访问失败;
        }
    }



0 0
原创粉丝点击