亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android下通過httpClient發(fā)送GET和POST請求的實例代碼

 更新時間:2013年08月27日 15:53:00   作者:  
這篇文章介紹了Android下通過httpClient發(fā)送GET和POST請求的實例代碼,有需要的朋友可以參考一下

復(fù)制代碼 代碼如下:

       public class HttpUtil {

    public static String sendDataByHttpClientGet(String path,String name,String pass){
        String result = "";
        //1.獲取到一個瀏覽器
        HttpClient client = new DefaultHttpClient();
        //2.準(zhǔn)備請求的地址
        try {
            String arg1 = URLEncoder.encode(name, "utf-8");
            String arg2 = URLEncoder.encode(pass, "utf-8");
            HttpGet httpGet = new HttpGet(path+"?name="+arg1+"&pass="+arg2);

            //3.敲回車發(fā)請求
            HttpResponse resp = client.execute(httpGet);
            //狀態(tài)碼
            int code = resp.getStatusLine().getStatusCode();
            if(code==200){
                //resp.getEntity().getContent();
                result = EntityUtils.toString(resp.getEntity(),"utf-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String sendDataByHttpClientPost(String path,String name,String pass){
        String result = "";
        //1獲取到一個瀏覽器
        HttpClient client = new DefaultHttpClient();

        //2.準(zhǔn)備要請求的數(shù)據(jù)類型
        HttpPost httpPost = new HttpPost(path);
        try {
            //鍵值對  NameValuePair
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name",name));
            params.add(new BasicNameValuePair("pass", pass));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");
            //3.設(shè)置POST請求數(shù)據(jù)實體
            httpPost.setEntity(entity);
            //4.發(fā)送數(shù)據(jù)給服務(wù)器
            HttpResponse resp = client.execute(httpPost);
            int code = resp.getStatusLine().getStatusCode();
            if(code==200){
                result = EntityUtils.toString(resp.getEntity(),"utf-8");
            }
        } catch (Exception e) {
        }
        return result;
    }

}

相關(guān)文章

最新評論