如何以Get方式获取Http服务器 上的数据,获取数据时,附加http头等验证信息,请看下面的代码:
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) { } }
主要用到的类:org.apache.http.client.HttpClient、org.apache.http.client.methods.HttpGet 和 org.json.JSONObject
Android 如何用HttpClient 以Get方式获取数据并添加http头信息
最新推荐文章于 2022-09-02 17:22:44 发布