Android中提供的HttpURLConnection和HttpClient接口能够用来开发HTTP程序。

  1. HttpURLConnection接口
        首先须要明确的是,Http通信中的POST和GET申请形式的不同。GET能够取得动态页面,也能够把参数放在URL字符串前面,传递给服务器。而POST办法的参数是放在Http申请中。因而,在编程之前,该当首先明确应用的申请办法,而后再依据所应用的形式抉择相应的编程形式。
        HttpURLConnection是继承于URLConnection类,二者都是抽象类。其对象次要通过URL的openConnection办法取得。创立办法如下代码所示:

    URL url = new URL("http://www.51cto.com/index.jsp?par=123456");  HttpURLConnection urlConn=(HttpURLConnection)url.openConnection(); 

       
    通过以下办法能够对申请的属性进行一些设置,如下所示:

 
 

//设置输出和输入流  urlConn.setDoOutput(true);  urlConn.setDoInput(true);  //设置申请形式为POST  urlConn.setRequestMethod("POST");  //POST申请不能应用缓存  urlConn.setUseCaches(false);  //敞开连贯  urlConn.disConnection();  

HttpURLConnection默认应用GET形式,例如上面代码所示:
 
 

//应用HttpURLConnection关上连贯                  HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();                  //失去读取的内容(流)                  InputStreamReader in = new InputStreamReader(urlConn.getInputStream());                  // 为输入创立BufferedReader                  BufferedReader buffer = new BufferedReader(in);                  String inputLine = null;                  //应用循环来读取取得的数据                  while (((inputLine = buffer.readLine()) != null))                  {                      //咱们在每一行前面加上一个"\n"来换行                      resultData += inputLine + "\n";                  }                           //敞开InputStreamReader                  in.close();                  //敞开http连贯                  urlConn.disconnect(); 

如果须要应用POST形式,则须要setRequestMethod设置。代码如下:
 
 

String httpUrl = "http://192.168.1.110:8080/httpget.jsp";          //取得的数据          String resultData = "";          URL url = null;          try         {              //结构一个URL对象              url = new URL(httpUrl);           }          catch (MalformedURLException e)          {              Log.e(DEBUG_TAG, "MalformedURLException");          }          if (url != null)          {              try             {                  // 应用HttpURLConnection关上连贯                  HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();                  //因为这个是post申请,设立须要设置为true                  urlConn.setDoOutput(true);                  urlConn.setDoInput(true);                  // 设置以POST形式                  urlConn.setRequestMethod("POST");                  // Post 申请不能应用缓存                  urlConn.setUseCaches(false);                  urlConn.setInstanceFollowRedirects(true);                  // 配置本次连贯的Content-type,配置为application/x-www-form-urlencoded的                  urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");                  // 连贯,从postUrl.openConnection()至此的配置必须要在connect之前实现,                  // 要留神的是connection.getOutputStream会隐含的进行connect。                  urlConn.connect();                  //DataOutputStream流                  DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());                  //要上传的参数                  String content = "par=" + URLEncoder.encode("ABCDEFG", "gb2312");                  //将要上传的内容写入流中                  out.writeBytes(content);                   //刷新、敞开                  out.flush();                  out.close();  
  1. HttpClient接口
        应用Apache提供的HttpClient接口同样能够进行HTTP操作。
        对于GET和POST申请办法的操作有所不同。GET办法的操作代码示例如下:
     
     

    // http地址          String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";          //HttpGet连贯对象          HttpGet httpRequest = new HttpGet(httpUrl);           //获得HttpClient对象              HttpClient httpclient = new DefaultHttpClient();              //申请HttpClient,获得HttpResponse              HttpResponse httpResponse = httpclient.execute(httpRequest);              //申请胜利              if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)              {                  //获得返回的字符串                  String strResult = EntityUtils.toString(httpResponse.getEntity());                  mTextView.setText(strResult);              }              else             {                  mTextView.setText("申请谬误!");              }          } 

    应用POST办法进行参数传递时,须要应用NameValuePair来保留要传递的参数。,另外,还须要设置所应用的字符集。代码如下所示:
     

 

// http地址          String httpUrl = "http://192.168.1.110:8080/httpget.jsp";          //HttpPost连贯对象          HttpPost httpRequest = new HttpPost(httpUrl);          //应用NameValuePair来保留要传递的Post参数          List<NameValuePair> params = new ArrayList<NameValuePair>();          //增加要传递的参数          params.add(new BasicNameValuePair("par", "HttpClient_android_Post"));          //设置字符集              HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312");              //申请httpRequest              httpRequest.setEntity(httpentity);              //获得默认的HttpClient              HttpClient httpclient = new DefaultHttpClient();              //获得HttpResponse              HttpResponse httpResponse = httpclient.execute(httpRequest);              //HttpStatus.SC_OK示意连贯胜利              if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)              {                  //获得返回的字符串                  String strResult = EntityUtils.toString(httpResponse.getEntity());                  mTextView.setText(strResult);              }              else             {                  mTextView.setText("申请谬误!");              }          } 

HttpClient实际上是对Java提供办法的一些封装,在HttpURLConnection中的输入输出流操作,在这个接口中被对立封装成了HttpPost(HttpGet)和HttpResponse,这样,就缩小了操作的繁琐性。
另外,在应用POST形式进行传输时,须要进行字符编码。