2015-09-03 2 views
0

У меня возникла проблема с значением «return» в классе Asynctask в doInBackground. Я получаю сообщение об ошибке «Отсутствие инструкции возврата в нижеприведенном коде».Проблемы с возвратом Asynctask для Android

`общественный класс ForecastNetwork расширяет AsyncTask {

public final String TAG = ForecastNetwork.class.getSimpleName(); 

    @Override 
    protected Void doInBackground(Void... params) { 

     HttpURLConnection urlConnection = null; 
     BufferedReader reader = null; 

// Будет содержать необработанный ответ JSON в виде строки. Строка прогнозаJsonStr = null;

 try { 
      // Construct the URL for the OpenWeatherMap query 
      // Possible parameters are avaiable at OWM's forecast API page, at 
      // http://openweathermap.org/API#forecast 
      URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7"); 

      // Create the request to OpenWeatherMap, and open the connection 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.connect(); 

      // Read the input stream into a String 
      InputStream inputStream = urlConnection.getInputStream(); 
      StringBuffer buffer = new StringBuffer(); 
      if (inputStream == null) { 
       // Nothing to do. 
       return null; 
      } 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 

      String line; 
      while ((line = reader.readLine()) != null) { 
       // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) 
       // But it does make debugging a *lot* easier if you print out the completed 
       // buffer for debugging. 
       buffer.append(line + "\n"); 
      } 

      if (buffer.length() == 0) { 
       // Stream was empty. No point in parsing. 
       return null; 
      } 
      forecastJsonStr = buffer.toString(); 
     } catch (IOException e) { 
      Log.e(TAG, "Error ", e); 
      // If the code didn't successfully get the weather data, there's no point in attemping 
      // to parse it. 
      return null; 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (final IOException e) { 
        Log.e(TAG, "Error closing stream", e); 
       } 
      } 
     } 


    }` 

Что я должен вернуться в конце?

ответ

0

Я предполагаю, что вы забыли вернуть результат обработки

forecastJsonStr = buffer.toString(); 
    return forecastJsonStr; 
+0

Должен ли я писать в конце? На самом деле, я делаю онлайн-курс разработчиками Google, занимаемыми udacity, и они ничего не сделали. – Rebecca

+0

Будет ли это иметь значение, если я изменюсь на параметр результата i.e public class ForecastTask extends в открытый класс ForecastTask extends ? Помоги мне. Я действительно нахожу наркотики. – Rebecca

+0

Спасибо. На самом деле это сработало, как вы сказали. :) – Rebecca

Смежные вопросы