zoukankan      html  css  js  c++  java
  • Android 中HttpURLConnection与HttpClient的简单使用

    1:HttpHelper.java

    public class HttpHelper {
        //1:标准的Java接口
        public static String getStringFromNet1(String param){
            String result="";
            try{
                URL url=new URL(param);
                HttpURLConnection conn=(HttpURLConnection)url.openConnection();
                if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
                    InputStream is=conn.getInputStream();
                    byte[]data=new byte[1024];
                    int len=is.read(data);
                    result=new String(data,0,len);
                    is.close();
                    conn.disconnect();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return result;
        }
        
        //2:Apache接口
        public static String getStringFromNet2(String param){
            String result="";
            try{
                HttpClient client=new DefaultHttpClient();
                HttpGet get=new HttpGet(param);
                HttpResponse response=client.execute(get);
                if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
                    result=EntityUtils.toString(response.getEntity());
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return result;
        }
    }
  • 相关阅读:
    北航算法作业三
    水库抽样
    python命名空间
    我说
    Fn键
    windows批处理运行java程序
    Java Sound初探
    java.io.IOException: mark/reset not supported
    三层交换机对链路层数据帧的处理
    北航数值分析作业三
  • 原文地址:https://www.cnblogs.com/yshyee/p/3370147.html
Copyright © 2011-2022 走看看