android平台HttpGet、HttpPost请求实例

2019-12-10 20:05:32于海丽

        TextView resultText = (TextView) this.findViewById(R.id.resultText);                  
        String username="username";  
        String password="password";  
        String httpUrl = "http://www.easck.com/AndroidLogin/loginAction.do?method=login"; 

        //创建httpRequest对象  
        HttpPost httpRequest = new HttpPost(httpUrl);  

        List<NameValuePair> params = new ArrayList<NameValuePair>();  
        params.add(new BasicNameValuePair("username", username));  
        params.add(new BasicNameValuePair("password", password)); 

        try{  
            //设置字符集  
            HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312"); 

            //请求httpRequest  
            httpRequest.setEntity(httpentity); 

            //取得默认的HttpClient  
            HttpClient httpclient = new DefaultHttpClient(); 

            //取得HttpResponse  
            HttpResponse httpResponse = httpclient.execute(httpRequest); 

            //HttpStatus.SC_OK表示连接成功  
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
                //取得返回的字符串  
                String strResult = EntityUtils.toString(httpResponse.getEntity());