首页 > 代码库 > Android攻城狮httpClient post/get传递参数
Android攻城狮httpClient post/get传递参数
1 public class RegistActivity extends Activity { 2 3 private EditText name, age; 4 private Button button; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 // TODO Auto-generated method stub 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.register); 11 name = (EditText) findViewById(R.id.editText2name); 12 age = (EditText) findViewById(R.id.editText1age); 13 button = (Button) findViewById(R.id.button1); 14 15 button.setOnClickListener(new OnClickListener() { 16 17 @Override 18 public void onClick(View v) { 19 // TODO Auto-generated method stub 20 String url="http://172.16.166.195:8080/web/Myservlet"; 21 // new HttpThread1(url, name.getText().toString(), age.getText().toString()).start(); 22 url = url + "?name=" +name.getText().toString() + "&age=" + age.getText().toString(); 23 new HttpClientThread(url).start();//调dohttpClientGet(); 24 new HttpClientThread(url,name.getText().toString(),age.getText().toString()).start();//调dohttpClientPost(); 25 } 26 }); 27 } 28 29 }
1 public class HttpClientThread extends Thread { 2 private String url; 3 private String name; 4 private String age; 5 6 public HttpClientThread(String url) { 7 // TODO Auto-generated constructor stub 8 this.url = url; 9 } 10 11 public HttpClientThread(String url, String name, String age) { 12 // TODO Auto-generated constructor stub 13 this.url = url; 14 this.name = name; 15 this.age = age; 16 } 17 18 private void dohttpClientPost() { 19 HttpClient client = new DefaultHttpClient(); 20 HttpPost post = new HttpPost(url); 21 // 通过NameValuePair去存储数据 22 ArrayList<NameValuePair> list = new ArrayList<NameValuePair>(); 23 list.add(new BasicNameValuePair("name", name)); 24 list.add(new BasicNameValuePair("age", age)); 25 26 try { 27 // 设置要发送的数据 28 post.setEntity(new UrlEncodedFormEntity(list)); 29 HttpResponse response = client.execute(post); 30 31 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 32 String content = EntityUtils.toString(response.getEntity()); 33 } 34 } catch (UnsupportedEncodingException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } catch (ClientProtocolException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } catch (IOException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 45 } 46 47 private void dohttpClientGet() { 48 // 创建HttpGet对象 49 HttpGet httpGet = new HttpGet(url); 50 // 创建Httpclient对象 51 HttpClient client = new DefaultHttpClient(); 52 53 HttpResponse response; 54 55 try {// 发送请求 56 response = client.execute(httpGet); 57 // 判断类型 58 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 59 // 取出服务器返回的数据 60 String content = EntityUtils.toString(response.getEntity()); 61 62 System.out.println("--->content" + content); 63 64 } 65 } catch (ClientProtocolException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } catch (IOException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 } 72 73 } 74 75 @Override 76 public void run() { 77 // TODO Auto-generated method stub 78 // dohttpClientGet(); 79 dohttpClientPost(); 80 } 81 }
Android攻城狮httpClient post/get传递参数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。