如何验证天气应用程序的邮政编码和城市/州

时间:2016-07-30 20:42:40

标签: android

所以我现在被困在验证我的天气应用程序的邮政编码搜索主题上。我还想验证城市和州搜索。例如,当人们搜索邮政编码时,我只想要5位数的邮政编码。对于这个城市,我在搜索城市时不想要任何数字。 有人能帮我吗?

public class WeatherRequestTask extends AsyncTask<String, Integer, ResponseDto> {
    private static final String WU_HOST = "http://api.wunderground.com";
    private static final String WU_API_KEY = "/api/36053e2befabcfe2";
    private static final String WU_DATA_FEATURE_CONDITION = "/conditions";
    private static final String WU_QUERY_TAG = "/q";
    private static final String WU_API_EXT = ".json";

    private String mZipCode;
    private String mCity;
    private String mState;

    public WeatherRequestTask(String zipCode) {
        if (TextUtils.isEmpty(zipCode)) {
            throw new IllegalArgumentException("Zip Code is required");
        }
        mZipCode = zipCode;

    }

    public WeatherRequestTask(String city, String state) {
        mCity = city;
        mState = state;
        // TODO: validate city and state
    }

    @Override
    protected ResponseDto doInBackground(String... strings) {
        ResponseDto myResponseDto = new ResponseDto();
        HttpURLConnection urlConnection = null;
        try {

            URL url;
            if (TextUtils.isEmpty(mZipCode)) {
                // use city and state query
                url = new URL(WU_HOST + WU_API_KEY + WU_DATA_FEATURE_CONDITION + WU_QUERY_TAG + "/" + mState + "/" +
                        mCity + WU_API_EXT);
            } else {
                // use zipCode query
                url = new URL(WU_HOST + WU_API_KEY + WU_DATA_FEATURE_CONDITION + WU_QUERY_TAG + "/" + mZipCode +
                        WU_API_EXT);
            }

            urlConnection = (HttpURLConnection)url.openConnection();
            BufferedReader br = new BufferedReader((new InputStreamReader(urlConnection.getInputStream())));
            StringBuilder sb = new StringBuilder();

            String temp;
            while ((temp = br.readLine()) != null) {
                sb.append(temp);
            }

            JSONObject contentJson = new JSONObject(sb.toString());
            Response response = Response.deserialize(contentJson.getJSONObject("response"));
            CurrentObservation currentObservation = CurrentObservation.deserialize(contentJson.getJSONObject
                    ("current_observation"));
            myResponseDto.setCurrentObservation(currentObservation);
            myResponseDto.setResponse(response);
        } catch (Exception e) {
            return null;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return myResponseDto;
    }
}

2 个答案:

答案 0 :(得分:0)

对于这部分:

  

例如,当人们搜索时,我只想要5位数的邮政编码   邮政编码

您可以尝试检查邮政编码字符串的长度:

if(zipCode.length==5){

  //zipcode is 5 characters
  //proceed with the search

}else{

  //zipcode is less or more than 5 
  //warn user or whatever
}

并且:

  

对于这个城市,我在寻找这座城市时不需要任何数字。

if(mState.matches(".*\\d+.*"){

   //contains numbers
   //so don't carry forward or warn user

   }else{

   //Possibly what you want

   }

答案 1 :(得分:0)

这个问题有多个触角。什么&#34;验证&#34;意思?您可以购买数据库,将邮政编码映射到州/城市对;反问题有点复杂。 USPS出版了大量的书籍,提供了这些信息。

对于没有数字的城市,我从来没有听说过包含数字的城市名称 - 除了加利福尼亚的二十九棕榈树,但这只是一个字。