Android通过http方式获取JSON字符串并解析的注意事项(乱码,小黑框)
来源:互联网 发布:查询域名是不是被降权 编辑:程序博客网 时间:2024/06/10 08:33
在windows平台下换行符是 /r/n ,而在linux,android平台下换行符是 /n ,所以取得的JSON字符串必须进行过滤,将/r/n替换成/n,
另外不建议使用 BasicResponseHandler() ,这样会导致乱码
两个重要函数:
public static String get(String urlString) {/*try{HttpGet request = new HttpGet(urlString);String result=getHttpClient().execute(request,new BasicResponseHandler());return result;}catch(IOException e){throw e;}*/String result="";BufferedReader in=null;try { HttpClient client = new DefaultHttpClient(); HttpGet request=new HttpGet(urlString); HttpResponse response = client.execute(request); in=new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb=new StringBuffer(""); String line=""; String NL=System.getProperty("line.separator"); // String NL=""; while((line=in.readLine())!=null){ sb.append(line+NL); } in.close(); result=sb.toString(); result=JsonFilter(result);} catch (Exception e) {e.printStackTrace();}finally{if(in!=null){try{in.close();}catch(IOException e){e.printStackTrace();}}}return result;}/* * 对json字符串进行过滤,防止乱码和黑框 */public static String JsonFilter(String jsonstr){return jsonstr.substring(jsonstr.indexOf("{")).replace("\r\n","\n"); }
- Android通过http方式获取JSON字符串并解析的注意事项(乱码,小黑框)
- 解析HTTP POST方式请求的xml数据、本地XML文件数据, 并返回JSON字符串
- Android发送http请求并解析json
- [160824][Android]Android中JSON字符串的获取与解析
- Android获取assets文件夹下的json数据,并解析!
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- Android-通过URL获取网络资源 Json 字符串
- Android HttpURLConnection数据获取并JSON解析
- Android HttpClient获取JSON数据并解析
- Android 获取Google Weather API 并通过Xml和JSON解析数据
- Android 获取Google Weather API 并通过Xml和JSON解析数据
- java通过url得到json字符串并解析
- iOS 通过HTTP获取和解析XML或者JSON 的类
- wemall app中基于JAVA通过Http请求获取json字符串的代码
- asp通过json调用webservice接口,并获取返回的xml数据及解析
- jquery通过ajax方式获取json数据和解析
- 自定义键盘
- Remove all elements with values less than 50
- 彻底了解红黑树
- ListView 数据绑定、数据汇总及自定义排序示例
- 生产环境下的服务器的Crontab写法
- Android通过http方式获取JSON字符串并解析的注意事项(乱码,小黑框)
- discuz 在处理全局变量上的一点安全技巧
- Use find the search an element in deque
- QTP录制日历控件不能回放解决办法
- 架构设计师与SOA之基于SOA的架构
- Illustrating the generic search algorithm: Search for first occurrence of deque's contents as a subs
- Android 关闭整个应用程序
- 利用有限自动机进行字符串匹配
- 回调函数的作用与实现