【第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.404,39.975,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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的蹭蹭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值