Android中json解析
/** * 访问网络 * @param path * @param map * @return */ public static String post(String path,Map<String, Object> map){ DefaultHttpClient httpClient =new DefaultHttpClient(); String returnStr = ""; HttpPost httpPost = new HttpPost(path); HttpResponse resPonse = null; List<NameValuePair> params = new ArrayList<NameValuePair>(); if(map != null){ Set<String> keys = map.keySet(); Iterator<String> iter = keys.iterator(); while(iter.hasNext()){ String key = iter.next(); params.add(new BasicNameValuePair(key, map.get(key).toString())); } try { httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setStaleCheckingEnabled(httpParams, false); HttpConnectionParams.setConnectionTimeout(httpParams, 5000); HttpConnectionParams.setSoTimeout(httpParams, 5000); httpClient.setParams(httpParams); httpClient.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); resPonse = httpClient.execute(httpPost); if(resPonse.getStatusLine().getStatusCode() == 200){ returnStr = converString(resPonse.getEntity().getContent()); } if(returnStr != null && returnStr.equals("")){ returnStr = returnStr.replaceAll("\\\\\"", "\\\"").substring(1); returnStr = returnStr.substring(0, returnStr.length()-1); } } catch (Exception e) { }finally{ httpPost.abort(); } } return returnStr; } /** * 解析返回流 * @param is * @return */ public static String converString(InputStream is){ StringBuffer sb = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader(is), 512*1024); String line = ""; while((line = br.readLine()) != null){ sb.append(line); } } catch (Exception e) { // TODO: handle exception } return sb.toString(); }
相关推荐
nalanrumeng 2020-01-13
qianqianxiao 2019-11-04
Mexican 2011-10-12
翟浩浩Android 2013-01-13
leavesC 2019-06-29
88357660 2015-06-13
88377560 2014-03-31
kane 2014-03-31
88357660 2019-06-25
toperfect 2011-07-21
chinewwen 2016-08-17
SXIAOYI 2016-06-28
yinge00 2016-04-09
tcxingdechen 2015-11-27
Anything 2015-09-07
Urchindong 2015-03-26
家猫OR野猫 2014-12-11
pengjin 2014-12-10