胸怀万里世界, 放眼无限未来。这篇文章主要讲述Android攻城狮httpClient post/get传递参数相关的知识,希望能为你提供帮助。
1 public class RegistActivity extends Activity { 2 3private EditText name, age; 4private Button button; 5 6@Override 7protected void onCreate(Bundle savedInstanceState) { 8// TODO Auto-generated method stub 9super.onCreate(savedInstanceState); 10setContentView(R.layout.register); 11name = (EditText) findViewById(R.id.editText2name); 12age = (EditText) findViewById(R.id.editText1age); 13button = (Button) findViewById(R.id.button1); 14 15button.setOnClickListener(new OnClickListener() { 16 17@Override 18public void onClick(View v) { 19// TODO Auto-generated method stub 20String url="http://172.16.166.195:8080/web/Myservlet"; 21//new HttpThread1(url, name.getText().toString(), age.getText().toString()).start(); 22url = url + "?name=" +name.getText().toString() + "& age=" + age.getText().toString(); 23new HttpClientThread(url).start(); //调dohttpClientGet(); 24new HttpClientThread(url,name.getText().toString(),age.getText().toString()).start(); //调dohttpClientPost(); 25} 26}); 27} 28 29 }
【Android攻城狮httpClient post/get传递参数】
1 public class HttpClientThread extends Thread { 2private String url; 3private String name; 4private String age; 5 6public HttpClientThread(String url) { 7// TODO Auto-generated constructor stub 8this.url = url; 9} 10 11public HttpClientThread(String url, String name, String age) { 12// TODO Auto-generated constructor stub 13this.url = url; 14this.name = name; 15this.age = age; 16} 17 18private void dohttpClientPost() { 19HttpClient client = new DefaultHttpClient(); 20HttpPost post = new HttpPost(url); 21// 通过NameValuePair去存储数据 22ArrayList< NameValuePair> list = new ArrayList< NameValuePair> (); 23list.add(new BasicNameValuePair("name", name)); 24list.add(new BasicNameValuePair("age", age)); 25 26try { 27// 设置要发送的数据 28post.setEntity(new UrlEncodedFormEntity(list)); 29HttpResponse response = client.execute(post); 30 31if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 32String content = EntityUtils.toString(response.getEntity()); 33} 34} catch (UnsupportedEncodingException e) { 35// TODO Auto-generated catch block 36e.printStackTrace(); 37} catch (ClientProtocolException e) { 38// TODO Auto-generated catch block 39e.printStackTrace(); 40} catch (IOException e) { 41// TODO Auto-generated catch block 42e.printStackTrace(); 43} 44 45} 46 47private void dohttpClientGet() { 48// 创建HttpGet对象 49HttpGet httpGet = new HttpGet(url); 50// 创建Httpclient对象 51HttpClient client = new DefaultHttpClient(); 52 53HttpResponse response; 54 55try {// 发送请求 56response = client.execute(httpGet); 57// 判断类型 58if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 59// 取出服务器返回的数据 60String content = EntityUtils.toString(response.getEntity()); 61 62System.out.println("---> content" + content); 63 64} 65} catch (ClientProtocolException e) { 66// TODO Auto-generated catch block 67e.printStackTrace(); 68} catch (IOException e) { 69// TODO Auto-generated catch block 70e.printStackTrace(); 71} 72 73} 74 75@Override 76public void run() { 77// TODO Auto-generated method stub 78// dohttpClientGet(); 79dohttpClientPost(); 80} 81 }
推荐阅读
- Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)(子线程 与 子线程之间)
- spring整合hibernate的applicationContext.xml文件配置以及web.xml
- Android环境搭建 NDK+ADT(免cywgin)
- Android开发怎么让自己的APP UI漂亮大方(规范篇一)
- Android开发怎么让自己的APP UI漂亮大方(配色篇二)
- Android驱动之 Linux Input子系统之TP——A/B(Slot)协议
- Android攻城狮 调试
- android studio 如何设置代码提示
- Android PagerSlidingTitleIconTabStrip 能够在title旁边加小图标的PagerSlidingTabStrip