Android通过http方式获取JSON字符串并解析的注意事项(乱码,小黑框)
2012-08-10 17:22
836 查看
在windows平台下换行符是 /r/n ,而在linux,android平台下换行符是 /n ,所以取得的JSON字符串必须进行过滤,将/r/n替换成/n,
另外不建议使用 BasicResponseHandler() ,这样会导致乱码
两个重要函数:
另外不建议使用 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字符串并解析的注意事项(乱码,小黑框)
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android通过httpClient请求获取JSON数据并且解析
- android proguard注意事项(json的解析请注意)
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- android:通过url向网络端获取json格式数据并解析显示
- 通过GSON 解析Json数据,但是不创建JAVA Bean的方式,获取对应的值
- Android通过HttpURLConnection获取JSON并进行UI更新
- php解析http获取的json字符串变量总是空白null
- eval解析JSON中的注意点 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数。 2. 使用Function对象来进行返回解析。 使用eval
- android获取存储目录(路径)的几种方式和注意事项
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- [Android]四种方式解析字符串----JSON、SAX、DOM、XML