GET与POST方法
1. HTTP请求的种类
2. GET与POST方法的区别
3. 使用POST向服务器发送数据
1. HTTP请求的种类
现在最常用的就是 GET 和 POST 方法
2. GET与POST方法的区别
发送请求用问号?, 后面加上键值对. 多个键值对用&连接
3. 使用POST向服务器发送数据
上次访问marschen网站, 上传下载数据. 现在学习如何自己搭建本地服务器
使用tomcat, 官方下载 http://tomcat.apache.org/
压缩包解压之后
使用之前, 先配置Java的环境变量
回到tomcat的bin目录中, 双击startup.bat
服务器就建立了
首先要知道自己的ip地址, 我的是192.168.1.102
在浏览器中输入 http://192.168.1.102:8080/
当看到如下页面即代表成功
为了直接在电脑浏览器上测试下, 我们需要在如下路径中添加一个jsp文件, 文件内容如下
1 <% 2 String name = request.getParameter("name"); 3 String pwd = request.getParameter("password"); 4 5 out.print("name:" + name + ",password:" + pwd); //在浏览器上显示输入地址中的用户名与密码 6 %>
比如我们在地址栏输入 http://192.168.1.102:8080/s02e14.jsp?name=lisi&password=adc12345
就会显示如下内容
可知环境我们是搭建好了
接下来我们就要用Android程序来访问该地址
3. 分别使用GET和POST访问服务器
GET方法
1 public static class PlaceholderFragment extends Fragment { 2 3 private Button button; 4 private EditText nameText; 5 private EditText passwordText; 6 7 public PlaceholderFragment() { 8 } 9 10 @Override 11 public View onCreateView(LayoutInflater inflater, ViewGroup container, 12 Bundle savedInstanceState) { 13 View rootView = inflater.inflate(R.layout.fragment_main, container, false); 14 15 nameText = (EditText)rootView.findViewById(R.id.nameText); 16 passwordText = (EditText)rootView.findViewById(R.id.passwordText); 17 18 button = (Button)rootView.findViewById(R.id.submitButton); 19 ButtonListener buttonListener = new ButtonListener(); 20 button.setOnClickListener(buttonListener); 21 22 return rootView; 23 } 24 25 class ButtonListener implements OnClickListener{ 26 @Override 27 public void onClick(View v) { 28 String name = nameText.getText().toString(); 29 String pwd = passwordText.getText().toString(); 30 31 GetNetworkThread gnt = new GetNetworkThread(name,pwd); 32 gnt.start(); 33 } 34 } 35 36 class GetNetworkThread extends Thread{ 37 38 String GetName; 39 String GetPwd; 40 41 public GetNetworkThread(String name,String pwd){ //重载,可参看Java4Android的面向对象(4)和this的用法 42 this.GetName = name; 43 this.GetPwd = pwd; 44 } 45 46 @Override 47 public void run() { 48 HttpClient httpClient = new DefaultHttpClient(); 49 String url = "http://192.168.1.102:8080/s02e14.jsp?name=" + GetName + "&password=" + GetPwd; 50 HttpGet httpGet = new HttpGet(url); 51 try { 52 HttpResponse response = httpClient.execute(httpGet); 53 if(response.getStatusLine().getStatusCode() == 200){ 54 HttpEntity entity = response.getEntity(); 55 BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); 56 String result = reader.readLine(); 57 Log.d("HTTP","result:" + result); 58 } 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 } 63 }
GET方法提交请求大小, 模式都受到限制.
POST与GET最大的区别就是POST有请求体, 而GET没有
POST方法
1 public static class PlaceholderFragment extends Fragment { 2 3 private Button button; 4 private EditText nameText; 5 private EditText passwordText; 6 7 public PlaceholderFragment() { 8 } 9 10 @Override 11 public View onCreateView(LayoutInflater inflater, ViewGroup container, 12 Bundle savedInstanceState) { 13 View rootView = inflater.inflate(R.layout.fragment_main, container, false); 14 15 nameText = (EditText)rootView.findViewById(R.id.nameText); 16 passwordText = (EditText)rootView.findViewById(R.id.passwordText); 17 18 button = (Button)rootView.findViewById(R.id.submitButton); 19 ButtonListener buttonListener = new ButtonListener(); 20 button.setOnClickListener(buttonListener); 21 22 return rootView; 23 } 24 25 class ButtonListener implements OnClickListener{ 26 @Override 27 public void onClick(View v) { 28 String name = nameText.getText().toString(); 29 String pwd = passwordText.getText().toString(); 30 31 PostNetworkThread pnt = new PostNetworkThread(name,pwd); 32 pnt.start(); 33 } 34 } 35 36 class PostNetworkThread extends Thread{ 37 38 String PostName; 39 String PostPwd; 40 41 public PostNetworkThread(String name,String pwd){ 42 this.PostName = name; 43 this.PostPwd = pwd; 44 } 45 46 @Override 47 public void run() { 48 HttpClient httpClient = new DefaultHttpClient(); 49 String url = "http://192.168.1.102:8080/s02e14.jsp"; 50 //生成使用POST方法的请求对象 51 HttpPost httpPost = new HttpPost(url); 52 //NameValuePair对象代表一个需要发往服务器端额键值对 53 NameValuePair pairname = new BasicNameValuePair("name", PostName); 54 NameValuePair pairpassword = new BasicNameValuePair("password", PostPwd); 55 //将准备好 的键值对放置在一个List当中 56 ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); 57 pairs.add(pairname); 58 pairs.add(pairpassword); 59 try { 60 //创建代表请求体 61 HttpEntity requestEntity = new UrlEncodedFormEntity(pairs); 62 //将请求体放置在请求对象当中 63 httpPost.setEntity(requestEntity); 64 //执行请求对象 65 try { 66 HttpResponse response = httpClient.execute(httpPost); 67 if(response.getStatusLine().getStatusCode() == 200){ 68 HttpEntity entity = response.getEntity(); 69 BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); 70 String result = reader.readLine(); 71 Log.d("HTTP","Post:" + result); 72 } 73 } catch (Exception e) { 74 e.printStackTrace(); 75 } 76 } catch (Exception e) { 77 e.printStackTrace(); 78 } 79 } 80 } 81 }
总结;
POST提交数据的步骤
处理数据和GET是一样的