All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.haoxuer.discover.area.helpers.BaiduAreaService Maven / Gradle / Ivy

There is a newer version: 3.3.18-20230117
Show newest version
package com.haoxuer.discover.area.helpers;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.haoxuer.discover.area.data.entity.Area;

import java.util.ArrayList;
import java.util.List;

import jodd.http.HttpBrowser;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;

public class BaiduAreaService {
  
  public static void main(String[] args) {
    
    
    String id = "8258";
    List areas = shangquan(id);
    
    for (Area area : areas) {
      System.out.println(area);
    }
  }
  
  public static List area(String id) {
    List areas = new ArrayList();
    HttpBrowser browser = new HttpBrowser();
    browser.setDefaultHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0");
    HttpRequest request = HttpRequest
        .get("http://api.map.baidu.com/shangquan/forward/?qt=sub_area_list&ext=1&level=1&areacode=" + id + "&business_flag=0");
    
    HttpResponse response = browser.sendRequest(request);
    if (response.statusCode() == 200) {
      String body = browser.getPage();
      x(areas, body);
      
    }
    return areas;
  }
  
  private static void x(List areas, String body) {
    JsonParser parser = new JsonParser();
    JsonElement element = parser.parse(body);
    JsonElement content = element.getAsJsonObject().get("content");
    if (content == null) {
      return;
    }
    
    JsonElement sub = content.getAsJsonObject().get("sub");
    if (sub != null) {
      JsonArray array = sub.getAsJsonArray();
      for (JsonElement jsonElement : array) {
        JsonObject object = jsonElement.getAsJsonObject();
        String name = object.get("area_name").getAsString();
        Integer type = object.get("area_type").getAsInt();
        Integer code = object.get("area_code").getAsInt();
        String geo = object.get("geo").getAsString();
        Area area = new Area();
        area.setName(name);
        area.setCode(code + "");
        areas.add(area);
      }
    }
  }
  
  public static List shangquan(String id) {
    List areas = new ArrayList();
    HttpBrowser browser = new HttpBrowser();
    browser.setDefaultHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0");
    HttpRequest request = HttpRequest
        .get("http://api.map.baidu.com/shangquan/forward/?qt=sub_area_list&ext=1&level=1&areacode=" + id + "&business_flag=1");
    
    HttpResponse response = browser.sendRequest(request);
    if (response.statusCode() == 200) {
      x(areas, browser.getPage());
      
    }
    return areas;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy