从一个数据开放平台获取JSON(天气)
这是get的代码
这是get的代码
HttpClient httpClient=new DefaultHttpClient(); HttpGet httpGet=new HttpGet( "http://route.showapi.com/9-7"+"?"+"area=北京&areaid=&month=201606&showapi_appid=22661&showapi_sign=fa597c55d97d435ebbc67bc9dd45fe83"); HttpResponse response = httpClient.execute(httpGet); int code = response.getStatusLine().getStatusCode(); if(code== HttpStatus.SC_OK){ HttpEntity entity = response.getEntity() String s1 = EntityUtils.toString(entity, "utf-8"); System.out.println(s1); }
这个是post的
HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost=new HttpPost("http://route.showapi.com/9-7"); List<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>(); list.add(new BasicNameValuePair("showapi_sign","fa597c55d97d435ebbc67bc9dd45fe83")); list.add(new BasicNameValuePair("showapi_appid","22661")); list.add(new BasicNameValuePair("area","北京")); list.add(new BasicNameValuePair("month","201607")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ HttpEntity entity1 = response.getEntity(); String s1 = EntityUtils.toString(entity1, "utf-8"); System.out.println(s1); }
get得到的数据正确
post得到
{“showapi_res_code”:-3,”showapi_res_error”:”系统异常。读取错误。”,”showapi_res_body”:{}}
解决方案
40
应该是接口只支持GET请求吧