get请求

来源:互联网 发布:联动mpos 机报件软件 编辑:程序博客网 时间:2024/06/05 01:08
/** * GET方式请求网络 */private void httpGet(String name) {// HttpClient得到HttpClient对象HttpClient httpClient = new DefaultHttpClient();// 以get方式请求.并设置接口地址HttpGet httpGet = new HttpGet(path + "consName=" + name+ "&type=year&key=3ac9f31ff66b9746539472887b3799c3");try {// 连接网络请求数据,请求到的数据在httpResponse对象里HttpResponse httpResponse = httpClient.execute(httpGet);// 服务器返回的内容在HttpEntityHttpEntity entity = httpResponse.getEntity();// 把entity转成StringString string = EntityUtils.toString(entity);// System.out.println(string);Gson gson = new Gson();Bean bean = gson.fromJson(string, Bean.class);Message msg = Message.obtain();msg.obj = bean;handler.sendMessage(msg);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

0 0