当前位置:首页 > 开发 > 移动开发 > 正文

Android中json解析

发表于: 2012-05-23   作者:AndroidLL   来源:转载   浏览:
摘要:   /** * 访问网络 * @param path * @param map * @return */ public static String post(String path,Map<String, Object> map){ DefaultHttpClient httpClient

 

 /**
     * 访问网络
     * @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();
    }

Android中json解析

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号