在开发腾讯微博应用的时候,发现在用TestCase测试腾讯收索接口的时候,json能正常解析。
但是放入web应用中,发现腾讯传过来的数据出现乱码,用JSONObject也无法解析。
json_data=new String(json_data.getBytes(),"utf-8"); 用utf-8转码时,不能完全转成中文,中间会有??
在网上查了一些资料,要在刚传入时转码,能成功,但现在还不知道为什么。
修改 QHttpClient.java中的httpGet方法
try { /*byte[] b=new byte[2048];//这个是以前的代码 GZIPInputStream gzin = new GZIPInputStream(response.getEntity().getContent()); int length=0; while((length=gzin.read(b))!=-1){ responseData.append(new String(b,0,length)); } gzin.close();*/ //在这里进行转码 BufferedReader br = new BufferedReader(new InputStreamReader(new GZIPInputStream(response.getEntity().getContent()),"utf-8")); String s; while ((s = br.readLine()) != null ) { responseData.append(s); } br.close(); } catch (Exception e) { e.printStackTrace(); } finally { httpGet.abort(); } 收索接口测试 String format="json";//返回数据的格式 String contenttype="0";//消息的正文类型(按位使用) String keyword="红旗9"; //搜索关键字(1-128字节) String pagesize="20";//每页大小(1-30个) String page="1";//页码 String searchtype="0";//搜索类型 String msgtype="1";//消息的类型(按位使用) String sorttype ="0";//排序方式 String starttime="";//开始时间,用UNIX时间表示(从1970年1月1日0时0分0秒起至现在的总秒数) String endtime="";//结束时间,与starttime一起使用(必须大于starttime) String province="";//发表微博的用户所在的省代码。 String city="";//发表微博的用户所在的市代码。 String longitue="";//经度,(实数)*1000000 String latitude="";//纬度,(实数)*1000000 String radius="";//半径(整数,单位米,不大于20000) SearchAPI searchAPI= new SearchAPI(oAuth.getOauthVersion()); String json_data = searchAPI.t(oAuth, format, keyword, pagesize, page, contenttype, sorttype, msgtype, searchtype, starttime,endtime, province, city, longitue, latitude, radius); System.out.println(">>>> json_data: "+json_data); searchAPI.shutdownConnection(); JSONObject jsonObject =JSONObject.fromObject(json_data); System.out.println("jsonObject: "+jsonObject); //获取json数据 JSONObject data = jsonObject.getJSONObject("data"); List<TMbSessionMas> sessions = new ArrayList<TMbSessionMas>(); JSONArray infos = data.getJSONArray("info"); JSONObject info; Iterator it = infos.iterator(); while(it.hasNext()){ info = (JSONObject)it.next(); ....... sessions.add(session); }