2015-10-09 71 views
1

我正嘗試使用Weatherlib API創建天氣應用程序。我有下面的代碼。我試圖尋找城市模式,但不幸的是,okhttp引發了url == null的異常。未能在Weatherlib API中檢索搜索到的城市android

我創建了驗證,當至少輸入4個字母時開始搜索。一旦我輸入第四個字母,我得到這個例外...

有沒有人使用Weatherlib並遇到此問題?下面

  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) 

回答

0

ForecastIOProvider

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) { 

     } 
    }); 






} 

錯誤消息不支持通過城市名稱查詢,這樣的網址,你應該使用lat和長。 希望它可以幫助你!