你好,游客 登录 注册 搜索
背景:
阅读新闻

Android入门:发送HTTP的GET和POST请求

[日期:2012-07-22] 来源:Linux社区  作者:xiazdong [字体:大 中 小]

四、Android端代码

在AndroidManifest.xml加入:

<uses-permission android:name="android.permission.INTERNET"/>  

MainActivity.java

  1. package org.xiazdong.network.submit;  
  2.   
  3. import java.io.OutputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.URL;  
  6. import java.net.URLEncoder;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.EditText;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity {  
  17.     private EditText name, age;  
  18.     private Button getbutton, postbutton;  
  19.     private OnClickListener listener = new OnClickListener() {  
  20.         @Override  
  21.         public void onClick(View v) {  
  22.             try{  
  23.                 if (getbutton == v) {  
  24.                     /* 
  25.                      * 因为是GET请求,所以需要将请求参数添加到URL后,并且还需要进行URL编码 
  26.                      * URL = http://192.168.0.103:8080/Server/PrintServlet?name=%E6%88%91&age=20 
  27.                      * 此处需要进行URL编码因为浏览器提交时自动进行URL编码 
  28.                      * */  
  29.                     StringBuilder buf = new StringBuilder("http://192.168.0.103:8080/Server/PrintServlet");  
  30.                     buf.append("?");  
  31.                     buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");  
  32.                     buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));  
  33.                     URL url = new URL(buf.toString());  
  34.                     HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  35.                     conn.setRequestMethod("GET");  
  36.                     if(conn.getResponseCode()==200){  
  37.                         Toast.makeText(MainActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();  
  38.                     }  
  39.                     else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();  
  40.                 }  
  41.                 if (postbutton == v) {  
  42.                     /* 
  43.                      * 如果是POST请求,则请求参数放在请求体中, 
  44.                      * name=%E6%88%91&age=12 
  45.                      *  
  46.                      * */  
  47.                     StringBuilder buf = new StringBuilder();  
  48.                     buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");  
  49.                     buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));  
  50.                     byte[]data = buf.toString().getBytes("UTF-8");  
  51.                     URL url = new URL("http://192.168.0.103:8080/Server/PrintServlet");  
  52.                     HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  53.                     conn.setRequestMethod("POST");  
  54.                     conn.setDoOutput(true); //如果要输出,则必须加上此句   
  55.                     OutputStream out = conn.getOutputStream();  
  56.                     out.write(data);  
  57.                     if(conn.getResponseCode()==200){  
  58.                         Toast.makeText(MainActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();  
  59.                     }  
  60.                     else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();  
  61.                 }  
  62.             }  
  63.             catch(Exception e){  
  64.                   
  65.             }  
  66.         }  
  67.     };  
  68.   
  69.     @Override  
  70.     public void onCreate(Bundle savedInstanceState) {  
  71.         super.onCreate(savedInstanceState);  
  72.         setContentView(R.layout.main);  
  73.         name = (EditText) this.findViewById(R.id.name);  
  74.         age = (EditText) this.findViewById(R.id.age);  
  75.         getbutton = (Button) this.findViewById(R.id.getbutton);  
  76.         postbutton = (Button) this.findViewById(R.id.postbutton);  
  77.         getbutton.setOnClickListener(listener);  
  78.         postbutton.setOnClickListener(listener);  
  79.     }  
  80. }  

linux
相关资讯       Android入门教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款