通過get和post方式向服務器發送請求
首先說一下get和post的區別
get請求方式是將提交的參數拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23&jjj=888;
但是這種形式對於那種比較隱私的參數是不適合的,而且參數的大小也是有限制的,一般是1K左右吧,對於上傳文件
就不是很適合。
post請求方式是將參數放在消息體內將其發送到服務器,所以對大小沒有限制,對於隱私的內容也比較合適。
在android中用get方式向服務器提交請求:
在android模擬器中訪問本機中的tomcat服務器時,注意:不能寫localhost,因為模擬器是一個單獨的手機系統,所以要寫真是的IP地址。
否則無法訪問到服務器。
//要訪問的服務器地址,下面的代碼是要向服務器提交用戶名和密碼,提交時中文先要經過URLEncoder編碼,因為模擬器默認的編碼格式是utf-8
//而tomcat內部默認的編碼格式是ISO8859-1,所以先將參數進行編碼,再向服務器提交。
private String address = "http://192.168.2.101:80/server/loginServlet"; public boolean get(String username, String password) throws Exception { username = URLEncoder.encode(username); // 中文數據需要經過URL編碼 password = URLEncoder.encode(password); String params = "username=" + username + "&password=" + password; //將參數拼接在URl地址后面 URL url = new URL(address + "?" + params); //通過url地址打開連接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //設置超時時間 conn.setConnectTimeout(3000); //設置請求方式 conn.setRequestMethod("GET"); //如果返回的狀態碼是200,則一切Ok,連接成功。 return conn.getResponseCode() == 200; } //這種方式我平時喜歡用的方式 //獲得要傳遞的數據 String username = et1.getText().toString(); String password = et2.getText().toString(); // 創建HttpGet對象 HttpGet request = new HttpGet(url +"name="+username+"&password="+password); // 使用execute方法發送HTTP GET請求,並返回HttpResponse對象 // DefaultHttpClient為Http客戶端管理類,負責發送請 HttpResponse response = httpClient.execute(request); // 判斷請求響應狀態碼,狀態碼為200表示服務端成功響應了客戶端的請求 if (response.getStatusLine().getStatusCode() == 200) { // 使用getEntity方法獲得返回結果 String data = EntityUtils.toString(response.getEntity(),"gbk"); //獲得Message對象 Message msg = handler.obtainMessage(1); //創建Bundle對象 Bundle bundle = new Bundle(); //用mes傳遞數據 msg.setData(bundle); //開啟Message對象 msg.sendToTarget(); }
//用post得值 public boolean post(String username, String password) throws Exception { username = URLEncoder.encode(username); // 中文數據需要經過URL編碼 password = URLEncoder.encode(password); String params = "username=" + username + "&password=" + password; byte[] data = params.getBytes(); URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(3000); //這是請求方式為POST conn.setRequestMethod("POST"); //設置post請求必要的請求頭 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 請求頭, 必須設置 conn.setRequestProperty("Content-Length", data.length + ""); // 注意是字節長度, 不是字符長度 conn.setDoOutput(true); // 准備寫出 conn.getOutputStream().write(data); // 寫出數據 return conn.getResponseCode() == 200; } //下面是我喜歡的方式 //把來傳遞的數據封裝到user對象中 User user = new User(); user.setUserName(et1.getText().toString()); user.setUserPass(et2.getText().toString()); //創建Post對象 HttpPost request = new HttpPost("http://10.0.2.2:8080/system/Servlet"); // 將需要傳遞的參數封裝到List<NameValuePair>類型的對象中 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", user.getUserName())); params.add(new BasicNameValuePair("password", user.getUserPass())); // 將封裝參數的對象存入request中,並設置編碼方式 request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // DefaultHttpClient為Http客戶端管理類,負責發送請求和接受響應 HttpResponse response = defaultHttpClient.execute(request); // 判斷請求響應狀態碼,狀態碼為200表示服務端成功響應了客戶端的請求 if (response.getStatusLine().getStatusCode() == 200){ // 使用getEntity方法獲得返回結果 String data = EntityUtils.toString(response.getEntity(),"gdk"); //創建bundle對象 Bundle bundle = new Bundle(); //用bundle對象來封裝data數據 bundle.putString("data", data); //創建Message對象 Message mes = handler.obtainMessage(1); //存儲bundle數據 mes.setData(bundle); mes.sendToTarget();