【问题标题】:Android Asynctask return problemsAndroid Asynctask 返回问题
【发布时间】:2015-09-03 07:50:20
【问题描述】:

我在 doInBackground 方法的 Asynctask 类中的值“返回”中遇到问题。我收到一个错误,关于“以下代码中缺少返回语句。

`公共类 ForecastNetwork 扩展 AsyncTask {

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

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

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

// 将包含原始 JSON 响应作为字符串。 字符串 forecastJsonStr = 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);
                }
            }
        }


    }`

最后我应该返回什么?

【问题讨论】:

    标签: android-asynctask return-value


    【解决方案1】:

    我假设你忘记返回处理结果

        forecastJsonStr = buffer.toString();
        return forecastJsonStr;
    

    【讨论】:

    • 一定要写在最后吗?实际上,我正在做 udacity 举办的 Google 开发人员的在线课程,他们还没有做过类似的事情。
    • 是否有关系,如果我更改为 Result 参数,即公共类 ForecastTask 扩展 到公共类 ForecastTask 扩展 ?帮我。我真的很生气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多