百度API调用实例

今天根据需求要从百度API中取出一些数据,这些操作包括:将坐标转换成百度坐标,根据转换的百度坐标进行特定的查询。

有需求的收藏下,免得下次手写浪费时间。

涉及到的操作有:JSON格式的字符解析,HTTP请求和获得请求数据,文件流的写入和读出等等。

奉上源代码,共享:

首先是入口函数:

static void Main(string[] args)
        {
            Console.WriteLine("坐标转换,信息查询开始......");
            string oFile = "C:\\Users\\MisterHu\\Desktop\\点加属性\\聚类点.txt";
            string iFile = "C:\\Users\\MisterHu\\Desktop\\点加属性\\result.txt";

            List<string> tCoords = new List<string>();//存转换前坐标
            List<string> cvtCoords = new List<string>();//存转换后坐标
            List<string> rstQuery = new List<string>();//存查询结果

            string sInfo; //临时字符变量
            StreamReader sr = new StreamReader(oFile, Encoding.Default);
            while ((sInfo = sr.ReadLine()) != null)
            {
                tCoords.Add(sInfo.Substring(sInfo.IndexOf(',')+1));
            }
            sr.Close();

            //转换成百度坐标,然后存放到List中
            BaiDuAPI.CoordConvert(tCoords, cvtCoords);

            //从List中取出每个坐标,然后调用API
            BaiDuAPI.PlaceAPI(cvtCoords, rstQuery);

            //写入文件。
            String strLine = null;         
            for (int i = 1; i <= 1000;++i )
            {
                strLine += i.ToString() + "," + tCoords[i - 1] + rstQuery[i - 1] + "\r\n";           
            }

            FileStream.WriteInFile(strLine, iFile);
            Console.WriteLine("转换,获取,完成!");
        }

字符串输入输出到文件:

 //输入输出字符到文件中
    public static class FileStream
    {
        public static void ReadFormFile(string path)
        {

        }

        public static void WriteInFile(string content,string path)
        {
            string iFile = "D:\\temp.txt";
            if (path == null) path = iFile;
            
            StreamWriter sw = new StreamWriter(path);
            sw.Write(content);
   
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值