【问题标题】:HTTP Get Request - Incompatible TypesHTTP 获取请求 - 不兼容的类型
【发布时间】:2017-12-13 18:53:51
【问题描述】:

我正在运行基于 Yahoo Weather API 的基本获取请求。这是来自 Youtube 的示例。我无法让它运行。我在 doInBackground 方法中的 HTTP 请求中出现运行时错误。 (见下文)。

我在手机上收到错误消息“Java.lang.boolean 类型的值 True 无法转换为 JSON 对象”。所以我需要返回一个字符串。但是当我将“行”的类型更改为字符串时,它会给出错误“不兼容的类型 - 需要 java.lang.String - 找到布尔值”。因此,来自 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);
}

【问题讨论】:

  • 你能在 logcat 中检查这些错误发生在哪些行吗?

标签: java android httprequest bufferedreader


【解决方案1】:

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

这将导致错误,因为您在检查变量名称时尝试设置变量名称。

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

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

【讨论】:

  • 我把它分开了,现在它可以工作了!非常感谢您的快速回复..
  • @NielsVanwingh 太棒了!如果我的解决方案对您有帮助,那么您能否通过单击投票计数器下方的小复选标记将我的答案标记为已接受?
【解决方案2】:

您的 while 循环似乎是错误的。 “line”应该是字符串类型,while循环应该是

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

【讨论】:

  • 是的,我试图解决它。问题是我无法将其更改为“字符串”,这又给出了另一个错误消息。显然,表达式需要一个布尔值来返回,而该方法需要一个字符串。解决方案是将它们拆分并变成一个 IF 循环。感谢您的努力和帮助!
猜你喜欢
  • 1970-01-01
  • 2018-12-30
  • 2013-02-20
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 2019-02-15
  • 2012-04-07
  • 1970-01-01
相关资源
最近更新 更多