微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【第25篇】通过org.json结合HttpClient解析的百度API附近的银行地方信息

package com.org.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.http.httpentity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ServerUtils {
	
	private static String content = "",line=System.getProperty("line.separator");//换行相当于\n 
	public static String getContent(String url){
		HttpClient client=new DefaultHttpClient();
		httpentity httpentity=null;
		String result="";
		try {
			HttpGet post=new HttpGet(url);
			HttpResponse httpResponse = client.execute(post);
		    httpentity=httpResponse.getEntity();
			if(httpentity!=null){
				result=EntityUtils.toString(httpentity,"UTF-8").trim();
				return result; 
			}
		} catch (Exception e) {
			e.printstacktrace();
		}finally{
			try {
				httpentity.consumeContent();
				 
			} catch (IOException e) {
				e.printstacktrace();
			}
		}
		return null;
	}
	/** 
     * 读文件流 
     * @param formPath从哪里读取的文件路径 
     * @return 
     */  
    public static String readerFile(String formPath) {  
        FileReader read = null;  
        BufferedReader reader = null;  
        try {  
            read = new FileReader(new File(formPath));  
            reader = new BufferedReader(read);  
            StringBuffer buffer = new StringBuffer("");  
            content = reader.readLine();  
            while (content != null) {  
                buffer.append(content).append(line);  
                content = reader.readLine();  
            }  
            return content = buffer.toString();//返回  
        } catch (Exception e) {  
            e.printstacktrace();  
        } finally {  
            try {  
                if (reader != null)reader.close();  
                if (read != null)read.close();  
            } catch (Exception e) {  
                e.printstacktrace();  
            }  
        }  
        return "";//没值就返回空  
    }  
      
	
	public static void main(String[] args) {
		  String Url="http://api.k780.com:88/?app=entry.qihu&domain=www.baidu.com&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
		  System.out.println(getContent(Url));
	}
}
package com.org.json;

import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONObject;

import com.org.utils.ServerUtils;

/**
 * @Author:liangjilong
 * @Date:2014-4-28
 * @Version:1.0
 * @Descript:从网络上抓去下来解析json数据
 */
public class TestJson1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		String key="D4tWvZgUrICf3oga0Q0uT5sk";	
		String url=	getUrl("银行",key);	//即搜索经纬度39.915,116.404,39.975,116.414的附近的银行
		String json = ServerUtils.getContent(url);

		JSONObject jsonObj = new JSONObject(json);

		String status = jsonObj.get("status").toString();// 解析status节点
		// System.out.println(status);

		String  results=jsonObj.get("results").toString();//results节点
	
		JSONArray  resultArrs=new JSONArray(results);
		 
		for (int i = 0; i < resultArrs.length(); i++) {
			JSONObject resultObj=(JSONObject)resultArrs.get(i);  
			String name="第"+i+resultObj.get("name");//results下的name节点
			
			String address=resultObj.getString("address");////results下的address节点
			
			if(resultObj.has("telephone")){//判断是否有这个节点
				String telephone=resultObj.getString("telephone");////results下的telephone节点
				System.out.println(telephone);
			}
			String detail_url=resultObj.get("detail_url").toString();
			if(resultObj.has("tag")){//判断是否有这个节点有就取出来
				String tag=resultObj.get("tag").toString();
			}
		
			JSONObject locationObj=(JSONObject)resultObj.get("location");//location节点
			
			String lat=locationObj.getString("lat");//location节点lat经度
			String lng=locationObj.getString("lng");//location节点lng伟度
			System.out.println(lng);
			
		}
		 
	}
	
	/**
	 * 
	 * @param keyWord搜索的地方
	 * @param key百度申请的ak密钥
	 * @return
	 * @throws Exception
	 */
	public static String getUrl(String keyWord,String key)throws Exception{
		StringBuffer buffer=new StringBuffer();
		buffer.append("http://api.map.baidu.com/place/search?");
		buffer.append("&query="+URLEncoder.encode(keyWord,"utf-8"));
		buffer.append("&bounds="+"39.915,116.414");//经纬度
		buffer.append("&output=json");//输出格式(JSON/XML)
		buffer.append("&key="+key);
		return buffer.toString();
	}

}


package com.org.json;

import org.json.JSONArray;
import org.json.JSONObject;

import com.org.utils.ServerUtils;

/**
 * @Author:liangjilong
 * @Date:2014-4-28
 * @Version:1.0
 * @Descript:从本地的文本读取文件下来解析json数据
 */
public class TestJson2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		
		String path=TestJson2.class.getClassLoader().getResource("json.txt").getFile();
		String json = ServerUtils.readerFile(path);

		JSONObject jsonObj = new JSONObject(json);

		String status = jsonObj.get("status").toString();// 解析status节点
		// System.out.println(status);

		String  results=jsonObj.get("results").toString();//results节点
	
		JSONArray  resultArrs=new JSONArray(results);
		 
		for (int i = 0; i < resultArrs.length(); i++) {
			JSONObject resultObj=(JSONObject)resultArrs.get(i);  
			String name="第"+i+resultObj.get("name");//results下的name节点
			
			String address=resultObj.getString("address");////results下的address节点
			
			if(resultObj.has("telephone")){//判断是否有这个节点
				String telephone=resultObj.getString("telephone");////results下的telephone节点
				System.out.println(telephone);
			}
			String detail_url=resultObj.get("detail_url").toString();
			if(resultObj.has("tag")){//判断是否有这个节点
				String tag=resultObj.get("tag").toString();
			}
			
			
			JSONObject locationObj=(JSONObject)resultObj.get("location");//location节点
			
			String lat=locationObj.getString("lat");//location节点lat经度
			String lng=locationObj.getString("lng");//location节点lng伟度
			System.out.println(lng);
			
		}
		 
	}

}
http://jilongliang.iteye.com/blog/2056243

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐