Android中HTTP几种请求和响应的代码实现

Android 网络编程中最经常遇到的就是Http请求,和处理返回值得到信息,达到网络交互的效果;
下面看几种最常用的网络请求:

一 Http GET请求 (200响应)

private void HttpGet() {
        try {
            URL url = new URL("http://hbydhw.bestv.com.cn:8082/" +
                    "EDS/jsp/AuthenticationURL?Action=Login&UserID=test2018030701");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            String readLine = "";
            if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
                InputStream is = connection.getInputStream();
                byte[] buffer = new byte[1024];
                int hasread = 0;
                while((hasread = is.read(buffer))>0){
                    readLine += new String(buffer,0,hasread);
                }
                JSONObject jsonObject = JSONObject.fromObject(readLine);
                String name = jsonObject.getString("name");
                int number = jsonObject.getInt("number");
            }else{
                //其他响应
            }

        }catch (MalformedURLException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

二 Http GET请求 (302响应)

/**
     * 302响应
     * 获取到响应头返回的
     * Location地址
     * 主机地址等信息
     */
    private void getHttp302() {
        try {
            URL myUrl = new URL("");
            HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            // 必须设置false,否则会自动redirect到Location的地址
            connection.setInstanceFollowRedirects(false);
            if(connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP){
                String location = connection.getHeaderField("Location");
                String temp = location.substring(location.indexOf("//") + 2, location.length());
                String returnip = temp.substring(0, temp.indexOf("/"));
                returnip = "http://" + returnip;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

三 Http POST请求

 /**
     * http POST
     * 响应
     */
    private void getHttpPost() {
        try {
            String uri = "http://hbydfh.bestv.com.cn:6060/aaa/services/rest/V2/AAA/xxxxxx";
            URL url = new URL(uri);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            connection.setRequestProperty("Accept", "application/json");
            //POST请求中写入参数
            OutputStream out1 = connection.getOutputStream();
            JSONObject jsonMacInput = new JSONObject();
            jsonMacInput.put("key", "value");
            out1.write((jsonMacInput.toString()).getBytes());
            out1.flush();
            out1.close();

            if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
                InputStream is1 = connection.getInputStream();
                byte[] buffer1 = new byte[1024];
                int hasread = 0;

                while ((hasread = is1.read(buffer1)) > 0) {
                    returnaccount += new String(buffer1, 0, hasread);
                }
                //解析返回值
                JSONObject jsonOutPut = JSONObject.fromObject(returnaccount);
                resultCode = jsonOutPut.getString("returnKey");

                is1.close();
                connection.disconnect();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Android中HTTP几种请求和响应的代码实现)