zoukankan      html  css  js  c++  java
  • 在android用Get方式发送http请求

    烦人的日子终于过去啦,终于又可以写博客啦,对自己的android学习做个总结,方便以后查看...。。。

    一、在android用Get方式发送http请求,使用的是java标准类,也比较简单。

    主要分以下几步:

    1.构造URL

    URL url = new URL(String path);

    2.设置连接

    httpURLConnection = (HttpURLConnection) url.openConnection();
    //超时时间
    httpURLConnection.setConnectTimeout(3000);
    //表示设置本次http请求使用GET方式
    httpURLConnection.setRequestMethod("GET");
    int responsecode = httpURLConnection.getResponseCode();//返回至为响应编号,如:HTTP_OK表示连接成功。
    3.获取返回数据

    if(responsecode == HttpURLConnection.HTTP_OK){
    inputStream = httpURLConnection.getInputStream();
    }   //得到inputStream 就好办啦。
    new InputStreamReader(inputStream,"utf-8")

    4.关闭连接

    void disconnect()

    二、下面通过一个简单的Demo实现get方式的请求:

    package com.http.get;  
      
    import java.io.BufferedReader;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.io.InputStreamReader;  
    import java.io.UnsupportedEncodingException;  
    import java.net.HttpURLConnection;  
    import java.net.MalformedURLException;  
    import java.net.URL;  
      
      
      
    public class HttpUtils {  
      
        private static String URL_PATH="http://www.baidu.com";  
            private static HttpURLConnection httpURLConnection = null;  
        public HttpUtils(){  
              
        }   
      
         public static void shuchu(){  
            InputStream inputStream = getInputStream();  
            String result;  
            try {  
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));  
                result = "";  
                String line = "";  
           try {  
                while((line = reader.readLine())!= null){  
                       result = result+ line;  
                   }  
         } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
       }  
              System.out.println(result);  
              httpURLConnection.disconnect();  
            } catch (UnsupportedEncodingException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
             
        }  
        /** 
         * 获取服务端的数据,以InputStream返回 
         * @return 
         */  
        public static InputStream getInputStream(){  
            InputStream inputStream = null;  
              
            try {  
                URL url = new URL(URL_PATH);  
                if(url != null){  
                    try {  
                        httpURLConnection = (HttpURLConnection) url.openConnection();  
                        //超时时间  
                        httpURLConnection.setConnectTimeout(3000);  
                        //表示设置本次http请求使用GET方式  
                        httpURLConnection.setRequestMethod("GET");  
                        int responsecode = httpURLConnection.getResponseCode();  
                          
                        if(responsecode == HttpURLConnection.HTTP_OK){  
                            inputStream = httpURLConnection.getInputStream();  
                        }  
                    } catch (IOException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                }  
            } catch (MalformedURLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            return inputStream;  
        }  
        public static void main(String[] args){  
            //保存文件到本地  
            //saveImageToDisk();  
            shuchu();  
        }  
    }  
    

      //因为get方式是这ava标准类,直接写的java程序,不过都一样,android中也是一样的。。

    简单的访问了百度,返回的就是百度搜索首页的源代码:图片一直上传不了。。。就不截图啦。

    正确返回的就是你在网页单击右键有个查看源代码,返回的就是它,输出的也是它,自己可以去对比下一样不,是一样的。。
  • 相关阅读:
    不可小视视图对效率的影响力
    Maximum Margin Planning
    PhysicsBased Boiling Simulation

    Learning Behavior Styles with Inverse Reinforcement Learning
    Simulating Biped Behaviors from Human Motion Data
    Nearoptimal Character Animation with Continuous Control
    Apprenticeship Learning via Inverse Reinforcement Learning
    回报函数学习的学徒学习综述
    Enabling Realtime Physics Simulation in Future Interactive Entertainment
  • 原文地址:https://www.cnblogs.com/jycboy/p/4736278.html
Copyright © 2011-2022 走看看