天下之事,必先处之难,而后易之。

Android使用HttpURLConnection和HttpClient请求服务器数据

    HTTP提交方式有多种,最常用的的就是POST和GET,另外还有PUT、DELETE、HEAD。好久没学习了,Android这边又生疏了,近三个月来毫无建树,整天都忙也没学到什么东西,打算继续学习Android!

1.0 HttpURLConnection

1.1 HttpURLConnection:GET

public boolean loginByGet(String path, String username , String password) throws Exception{  
          
        String url_path = path +"?username=" + URLEncoder.encode(username, "utf-8") + "&password="+password;  
          
        URL url = new URL(url_path);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
          
        conn.setRequestMethod("GET");  
        conn.setConnectTimeout(5000);  
        if(conn.getResponseCode() == 200){  
            return true;  
        }  
          
          
        return false;  
    }  

1.2 HttpURLConnection:POST

public boolean loginByPost(String path,String username , String password) throws Exception{  
  
        System.out.println("LoginService的loginByPost()");  
        URL url = new URL(path);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
          
        conn.setRequestMethod("POST");  
        conn.setConnectTimeout(5000);  
          
        String value = "username=" + username +"&" + "password=" +password;  
        byte[] entity = value.getBytes();  
           
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
        conn.setRequestProperty("Content-Length", entity.length + "");  
          
        conn.setDoOutput(true);  
        OutputStream os = conn.getOutputStream();  
        os.write(entity);  
          
        if(conn.getResponseCode() == 200){  
            return true;  
        }  
          
        return false;  
    }  

2.0 HttpClient

2.1 HttpClient:GET

public boolean loginByHttpClientGet(String path,String username , String password) throws Exception{  
          
          
        String value = path + "?username=" + username +"&password=" + password;   
        HttpClient httpClient =  new DefaultHttpClient();  
        HttpGet httpGet = new HttpGet(value);  
          
        HttpResponse httpResponse = httpClient.execute(httpGet);  
        if(httpResponse.getStatusLine().getStatusCode() == 200){  
            return true;  
        }  
          
        return false;  
    }  

2.2 HttpClient:POST

public boolean loginByHttpClientPost(String path,String username , String password)throws Exception{  
          
        HttpClient httpClient = new DefaultHttpClient();  
        HttpPost httpPost = new HttpPost(path);  
          
        List<NameValuePair> parameters = new ArrayList<NameValuePair>();  
        parameters.add(new BasicNameValuePair("username", username));  
        parameters.add(new BasicNameValuePair("password", password));  
          
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");  
        httpPost.setEntity(entity);  
        HttpResponse httpResponse = httpClient.execute(httpPost);  
        if(httpResponse.getStatusLine().getStatusCode() == 200){  
            return true;  
        }  
          
        return false;  
    }  

posted @ 2024-01-16 07:15  boonya  阅读(20)  评论(0)    收藏  举报  来源
我有佳人隔窗而居,今有伊人明月之畔。
轻歌柔情冰壶之浣,涓涓清流梦入云端。
美人如娇温雅悠婉,目遇赏阅适而自欣。
百草层叠疏而有致,此情此思怀彼佳人。
念所思之唯心叩之,踽踽彳亍寤寐思之。
行云如风逝而复归,佳人一去莫知可回?
深闺冷瘦独自徘徊,处处明灯影还如只。
推窗见月疑是归人,阑珊灯火托手思忖。
庐居闲客而好品茗,斟茶徐徐漫漫生烟。

我有佳人在水之畔,瓮载渔舟浣纱归还。
明月相照月色还低,浅近芦苇深深如钿。
庐山秋月如美人衣,画堂春阁香气靡靡。
秋意幽笃残粉摇曳,轻轻如诉画中蝴蝶。
泾水潺潺取尔浇园,暮色黄昏如沐佳人。
青丝撩弄长裙翩翩,彩蝶飞舞执子手腕。
香带丝缕缓缓在肩,柔美体肤寸寸爱怜。
如水之殇美玉成欢,我有佳人清新如兰。
伊人在水我在一边,远远相望不可亵玩。