public class UpdateCityThreadJob<V> implements Callable<String> {
// 电话号码 private String phoneNumber;
public UpdateCityThreadJob(String phoneNumber) { this.phoneNumber = phoneNumber; }
/** * @param phoneNumber 电话号码 * @return 城市 * @Description 获取 电话号码归属地邮政信息 */ private String getCityRepeat(String phoneNumber) { String city = ""; try { city = getCity(phoneNumber); } catch (Exception e) { // 请求失败,重试五次,(防止网络原因导致请求失败) for (int i = 0; i < 5; i++) { try { city = getCity(phoneNumber); } catch (Exception e1) { continue; } } } return city; }
/** * * @Description 获取城市 * @param phoneNumber 电话号码 * @return 城市 * @throws Exception */ private String getCity(String phoneNumber) throws Exception { // 请求第三方接口判断号码归属地 String result = HttpClient .doGet("http://apis.juhe.cn/mobile/get?phone=" + phoneNumber + "&dtype=xml&key=" + Constants.API_KEY); return result; }
@Override public String call() throws Exception { synchronized (phoneNumber) { // 获取城市 String city = getCityRepeat(phoneNumber); return city; } }
} |