• Android Http请求方法汇总


    最近对比了Android 中使用的JDK的HttpURLConnection和Apache的HttpClien访问网络资源,总结了Http协议的相关资料。如下:

    JDK的HttpURLConnection:

    (1)get请求

    public String executeHttpGet(){

           String result = null;

        URL url = null;

        HttpURLConnection connection;

        InputStreamReader in =null;

        try{

            url = new URL("http://10.0.2.2.:8888/data/get/?token=alexzhou");

            connection =(HttpURLConnection)url.openConnection();

            in = new InputStream(connection.getInputStream());

            BufferedReader bufferedReader = new BufferedReader(in);

            StringBuffer str = new StringBuffer();

            String line =null;

            while((line = bufferedReader.readLine()) != null){

               str.append(line);

    }

              }catch(Exception e){

               e.printStackTrace();

    }finally{

            if(connection != null){

           connection.disconnect();

    } if(in != null){

         try{

          in.close();

    }catch(IOException e){

      e.printStackTrace();

    }

    }

    }

        return result;

    }

    (2)post请求

    public String executeHttpPost(){

        String result = null;

        URL url =null;

        HttpURLConnection connection = null;

        InputStream in = null;

        try{

          url = new URL("http://10.0.2.2:8888/data/post");

          connection =(HttpURLConnection)url.openConnection();

          connection.setDoInput(true);

          connection.setDoOutput(true);

          connection.setRequestMethod("POST");

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

          connection.setRequestProperty("Charset","utf-8");

           DataOutputStream dop = new DataOutputStream(connection.getOutputStream());

          dop.writeBytes("token=alexzhou");

          dop.flush();

          dop.close();

          in = new InputStreamReader(connection.getInputStream());

          BufferedReader bufferedReader = new BufferedReader(in);

          StringBuffer str = new StringBuffer();

          String line = null;

          while((line = bufferedReader.readLine()) != null){

              str.append(line);

    }

                result = str.toString();

    }catch(Exception e){

    e.printStackTrace()

    }finally{

         if(connection != null){

             connection.disconnect();

    }if(in != null){

        try{

        in.close();

    }catch(IOException e){

    e.printStackTrace();

    }

    }

    }

    return result;

    } 

    如果参数中有中文的话,可以使用下面的方式进行编码解码:

    URLEncoder.encode("测试",“utf-8”);
    URLDecoder.decode("测试",“utf-8”);
    Apache的HttpClient:

    (1)get请求

    public String executeGet(){ 

         String result = null;

       BufferedReader reader = null;

       try{

             HttpClient client = new DefaultHttpClient();

         HttpGet request = new HttpGet();

         request.setURL(new URL("http://10.0.2.2:8888/data/get/?token=alexzhou"));

        HttpResponse respone =client.execute(request); 

        reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer strBuffer = new StringBuffer("");

         String line = null;

         while ((line = reader.readLine()) != null) {

              strBuffer.append(line);

       }

           result = strBuffer.toString();

       catch (Exception e) {

               e.printStackTrace();

            finally {

               if (reader != null) {

                 try {

                     reader.close();

                     reader = null;

                  catch (IOException e) {

                     e.printStackTrace();

                  }

                }

            }

            return result;

     

    }

    (2)post方式

    public String executePost() {       
         String result = null;
         BufferedReader reader = null;
        try {

            HttpClient client = new DefaultHttpClient();

            HttpPost request = new HttpPost();

            request.setURI(new URI("http://10.0.2.2:8888/data/post/"));

            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

            postParameters.add(new BasicNameValuePair("token""alexzhou"));

            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);

            request.setEntity(formEntity);

            HttpResponse response = client.execute(request);

            reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer strBuffer = new StringBuffer("");          
            String line = null;
            while ((line = reader.readLine()) != null) {
                 strBuffer.append(line);
              }
                 result = strBuffer.toString();
            catch (Exception e) {
                e.printStackTrace();
            finally {
                if (reader != null) {
                    try {
                        reader.close();
                        reader = null;
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
     
            return result;
        }


  • 相关阅读:
    vm中花屏的最直接解决办法
    【转】SQL SERVER中一些常见性能问题的总结
    好不容易把Head First Design Patterns下下来了,与大家分享一下
    没想到单位的bt下载的速度可以到这么快,满意了
    买了张水货的1Gsd卡
    商业缩略语汇总
    一些个人收集的书籍恢复下载
    sourceforge.net 是不是被封了?
    2009年我大学毕业了,我工作了,总结成长中的我的2009年 Fred
    Web Service的传输协议
  • 原文地址:https://www.cnblogs.com/yuanting/p/4919442.html
Copyright © 2020-2023  润新知