Android 如何用HttpClient 以Get方式获取数据并添加http头信息

jopen 13年前
     <span style="line-height:26px;font-family:Arial;color:#333333;font-size:14px;">如何以Get方式获取Http服务器 上的数据,获取数据时,附加http头等验证信息,请看下面的代码:<pre class="brush:java; toolbar: true; auto-links: false;">public void HttpGetData() {  try {   HttpClient httpclient = new DefaultHttpClient();   String uri = "http://www.yourweb.com";    HttpGet get = new HttpGet(uri);   //添加http头信息     get.addHeader("Authorization", "your token ");   get.addHeader("Content-Type", "application/json");   get.addHeader("User-Agent","your agent");   HttpResponse response;   response = httpclient.execute(get);   int code = response.getStatusLine().getStatusCode();   //检验状态码,如果成功接收数据    if (code == 200) {    //返回json格式: {"id": "27JpL~j4vsL0LX00E00005","version": "abc"}             String rev = EntityUtils.toString(response.getEntity());      obj = new JSONObject(rev);      String id = obj.getString("id");      String version = obj.getString("version");     }  } catch (Exception e) {    } }</pre></span>主要用到的类:org.apache.http.client.HttpClient、org.apache.http.client.methods.HttpGet    <span style="line-height:26px;font-family:Arial;color:#333333;font-size:14px;">和 org.json.JSONObject</span>