HTTP Get 请求 - 不兼容的类型

HTTP Get Request - Incompatible Types

我正在 运行基于 Yahoo Weather API 的基本获取请求。这是来自 Youtube 的示例。我无法将其发送到 运行.. 我在 doInBackground 方法中的 HTTP 请求中收到 运行time 错误。 (见下文)。

我在 phone "Value True of Type Java.lang.boolean cannot be converted into JSON Object" 上收到错误消息。所以我需要 return 一个字符串。但是当我将 "line" 的类型更改为 String 时,它给出了错误 "Incompatible types - required java.lang.String - found Boolean"。所以 BufferedReader 的 readline 命令需要一个字符串,但找到一个布尔值。任何人都可以向我解释发生了什么以及如何解决这个问题吗?

提前致谢!

public void refreshWeather(最终字符串位置){

    new AsyncTask<String, Void, String>()

    {
        @Override
        protected String doInBackground(String... strings) {

            String YQL = String.format("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"%s\")",location);
            String endpoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(YQL));

            try {
                URL url = new URL(endpoint);
                URLConnection connection = url.openConnection();
                InputStream inputStream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder result = new StringBuilder();
                boolean line;

                while ((line = reader.readLine()!= null)){
                    result.append(line);
                }

                return result.toString();

            } catch (Exception e) {
                error = e;
                }
            return null;
        }

        /**

         onPostExecute checks first if there is a service failure, then if city is valid, then it
         populates the data from the city and goes to method serviceSuccess and populates the fields with the retrieved data

         */

        @Override
        protected void onPostExecute(String s) {

            if (s == null && error != null){
                callback.serviceFailure(error);
                return;
            }

            try {
                JSONObject data = new JSONObject(s);
                JSONObject queryResults = data.optJSONObject("query");

                int count = queryResults.optInt("count");
                if (count == 0){

                    callback.serviceFailure(new LocationWeatherException("No Weather Info Found for" + location));
                    return;
                }


                Channel channel = new Channel();
                channel.populate(queryResults.optJSONObject("results").optJSONObject("channel"));

                callback.serviceSuccess(channel);

                } catch (JSONException e) {

                callback.serviceFailure(e);
            }
        }
    }.execute(location);
}

您的 while 循环似乎有误。 "line" 应该是字符串类型 while 循环应该是

while ((line = reader.readLine()) != null)

当您在 java 中进行比较时,您会检查 ==。进行比较时也不要设置变量,例如:line = reader.readLine()!= null

这会导致错误,因为当您尝试设置变量名称时检查它等于什么。

尝试在调用之前设置变量,然后使用 line != null 例如:

line = reader.readLine();
if(line != null) {
    //do something
}