在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"); }