您的位置:首页 > 理论基础 > 计算机网络

httpclient

2015-08-13 11:16 399 查看
可以通过谷歌自带的浏览器工具去分析,get与pos 的传递参数和方式等详细信息的分析。

自定义工具选项——更多工具——开发者工具来调出来,热键:ctrl+shift+i;

------------------------------------------------------------------------------

httpclient:

可以快速的进行访问服务器应用层的操作,http://hc.apache.org/downloads.cgi下载httpclient说明文档。

 

 

 

[b]------------------------------------------------------------------------------[/b]

用Httpclient传递参数:

 http://wallimn.iteye.com/blog/540566

 

----------------------------------------------------------------------------------------------------------

 

1、用get方式传参:---------------HttpClientThread01
界面

public class HttpClientThread01 extends Thread {

 private String url = null;

 public HttpClientThread01(String url) {

  this.url = url;

 }

 private void doHttpClientGet() {

  HttpGet httpGet = new HttpGet(url);

  HttpClient client = new DefaultHttpClient();

  HttpResponse response = null;

  try {

   response = client.execute(httpGet);

   if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

    String content = EntityUtils.toString(response.getEntity());

    System.out.println("content=========>" + content);

   }

  } catch (ClientProtocolException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 @Override

 public void run() {

  doHttpClientGet();

 }

}

-------------------------MainActivity---------------------

public void onClick(View v) {

    String name = usernameregister.getText().toString().trim();

    String pass = passwordregister.getText().toString().trim();

    String companystr = company.getText().toString().trim();

    String companyidstr = companyid.getText().toString().trim();

    String telephonestr = telephone.getText().toString().trim();

    String agestr = age.getText().toString().trim();

    String sexstr = ((RadioButton) RegisterActivity.this

      .findViewById(sexRegister.getCheckedRadioButtonId()))

      .getText().toString().trim();

     //调用HttpClientThread01(get)方法向服务器提交数据。

      String url=url + "?name=" + name + "&psd=" + pass + "&companyname=" + companystr + "&companyid=" + companyidstr + "&telephone=" + telephonestr + "&age=" + agestr + "&sex=" + sexstr; new HttpClientThread01(url).start(); finish();

 }

 ----------------------------------布局就是一个有主界面这些这些字段的布局,textview 加edittext

 

 

 2、用post的方法传参:

 

-------------------------MainActivity---------------------

public void onClick(View v) {

    String name = usernameregister.getText().toString().trim();

    String pass = passwordregister.getText().toString().trim();

    String companystr = company.getText().toString().trim();

    String companyidstr = companyid.getText().toString().trim();

    String telephonestr = telephone.getText().toString().trim();

    String agestr = age.getText().toString().trim();

    String sexstr = ((RadioButton) RegisterActivity.this

      .findViewById(sexRegister.getCheckedRadioButtonId()))

      .getText().toString().trim();

try {

//URLEncoder.enconde(name,"utf-8")的意思就是防止出现乱码,适应中文。

     new HttpClientThread01(url, URLEncoder

       .encode(name, "utf-8"), pass, URLEncoder.encode(

       companystr, "utf-8"), companyidstr, telephonestr,

       URLEncoder.encode(sexstr, "utf-8"), agestr).start();

    } catch (UnsupportedEncodingException e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

    }

}

 

 -------------------------HttpClientThread01---------------------

 

public class HttpClientThread01 extends Thread {

 private String url;

 // get

 public HttpClientThread01(String url) {

  this.url = url;

 }

 private String name, psd, companyname, companyid, telephone, sex, age;

 // post

 public HttpClientThread01(String url, String name, String psd,

   String companyname, String companyid, String telephone, String sex,

   String age) {

  this.url = url;

  this.name = name;

  this.psd = psd;

  this.companyname = companyname;

  this.companyid = companyid;

  this.telephone = telephone;

  this.sex = sex;

  this.age = age;

 }

 // doHttpClientPost

 private void doHttpClientPost() {

  // 创建HttpClient对象

  HttpClient client = new DefaultHttpClient();

  // 创建HttpPost对象

  HttpPost post = new HttpPost(url);

  // 发送请求 通过NameValuePair去存储数据的。

  ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();

  list.add(new BasicNameValuePair("name", name));

  list.add(new BasicNameValuePair("psd", psd));

  list.add(new BasicNameValuePair("companyname", companyname));

  list.add(new BasicNameValuePair("companyid", companyid));

  list.add(new BasicNameValuePair("telephone", telephone));

  list.add(new BasicNameValuePair("sex", sex));

  list.add(new BasicNameValuePair("age", age));

  try {

   // 设置要发送的数据

   post.setEntity(new UrlEncodedFormEntity(list));

   // 发送请求

   HttpResponse response = client.execute(post);

   // 判断如果发送的请求是SC_OK,表示请求服务器成功

   if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

    // 取出服务器返回的数据(调用EntityUtils.toString的方法,系统自动把getEntity做一系列转发存储到content中)

    String content = EntityUtils.toString(response.getEntity());

    //

    System.out.println("content=========>" + content);

   }

  } catch (UnsupportedEncodingException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (ClientProtocolException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 // doHttpClientGet

 private void doHttpClientGet() {

  // 创建HttpGet对象

  HttpGet get = new HttpGet(url);

  // 创建HttpClient对象

  HttpClient client = new DefaultHttpClient();

  HttpResponse response = null;

  try {

   // 发送请求

   response = client.execute(get);

   // 判断类型

   if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

    // 取出服务器返回的数据

    String content = EntityUtils.toString(response.getEntity());

    //

    System.out.println("content=========>" + content);

   }

  } catch (ClientProtocolException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 @Override

 public void run() {

  // doHttpClientGet();

  doHttpClientPost();

 }

}

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息