无法在Weatherlib API android上检索搜索到的城市

时间:2015-10-09 14:42:42

标签: java android android-studio

我正在尝试使用Weatherlib API创建天气应用。我在下面有这个代码。我正在尝试搜索城市模式,但不幸的是,okhttp提出了url == null的异常。

我创建了一个验证,以便在输入至少4个字母时开始搜索。一旦我输入第四个字母,我就会得到这个例外...

有没有人使用过Weatherlib并遇到过这个问题?

    WeatherClient.ClientBuilder builder = new WeatherClient.ClientBuilder();
    WeatherConfig config = new WeatherConfig();
    config.unitSystem = WeatherConfig.UNIT_SYSTEM.I;
    config.lang = "en"; // set language to english
    config.maxResult = 5; // max number of cities retrieved
    config.numDays = 6 ; //maximum days in the forecast


    try {
        client = builder.attach(this)
                .provider(new ForecastIOProviderType())
                .httpClient(WeatherDefaultClient.class)
                .config(config)
                .build();
    } catch (WeatherProviderInstantiationException e) {
        e.printStackTrace();
    }

    final EditText searchedText = (EditText) findViewById(R.id.searchText);

    searchedText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            search = searchedText.getText().toString();
            if (search.length() > 3)
            {
                list.clear();
                try {
                    System.out.println("testingbefore");  
                    // reaches this point for the last
                    client.searchCity(search.toString(), new WeatherClient.CityEventListener() {

                        @Override
                        public void onCityListRetrieved(List cities) {
                            // The data is ready
                            // does not reach this point
                            System.out.println("testing");
                            for (Object c : cities) {
                                list.add(((City) c).getName());
                            }
                        }

                        @Override
                        public void onWeatherError(WeatherLibException e) {
                            // Error on geting data
                            System.out.println("testingError");
                        }

                        @Override
                        public void onConnectionError(Throwable throwable) {// Connection error
                            System.out.println("testingConnection");
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });






}

以下错误消息

            10-09 15:35:24.681  11596-11596/ie.betterweather.betterweather I/System.out? testingbefore
            10-09 15:35:24.688  11596-11596/ie.betterweather.betterweather W/System.err? java.lang.IllegalArgumentException: url == null
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at com.squareup.okhttp.Request$Builder.url(Request.java:148)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at com.survivingwithandroid.weather.lib.client.okhttp.WeatherDefaultClient._doSearchCity(WeatherDefaultClient.java:149)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at com.survivingwithandroid.weather.lib.client.okhttp.WeatherDefaultClient.searchCity(WeatherDefaultClient.java:92)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at ie.betterweather.betterweather.MainActivity$1.onTextChanged(MainActivity.java:81)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.widget.TextView.sendOnTextChanged(TextView.java:7722)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.widget.TextView.handleTextChanged(TextView.java:7782)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9514)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:964)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:515)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:454)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:33)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:685)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:445)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:340)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
            10-09 15:35:24.689  11596-11596/ie.betterweather.betterweather W/System.err? at android.os.Handler.dispatchMessage(Handler.java:102)
            10-09 15:35:24.690  11596-11596/ie.betterweather.betterweather W/System.err? at android.os.Looper.loop(Looper.java:211)
            10-09 15:35:24.690  11596-11596/ie.betterweather.betterweather W/System.err? at android.app.ActivityThread.main(ActivityThread.java:5373)
            10-09 15:35:24.690  11596-11596/ie.betterweather.betterweather W/System.err? at java.lang.reflect.Method.invoke(Native Method)
            10-09 15:35:24.690  11596-11596/ie.betterweather.betterweather W/System.err? at java.lang.reflect.Method.invoke(Method.java:372)
            10-09 15:35:24.690  11596-11596/ie.betterweather.betterweather W/System.err? at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
            10-09 15:35:24.690  11596-11596/ie.betterweather.betterweather W/System.err? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

1 个答案:

答案 0 :(得分:0)

ForecastIOProvider不支持按城市名称查询,所以url,你应该使用lat和long。 希望它能帮到你!