当前位置: 首页 > 知识库问答 >
问题:

HTTP Get请求-不兼容的类型

鞠侯林
2023-03-14

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);
}

共有1个答案

晋功
2023-03-14

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

这将导致一个错误,因为当您试图设置变量名时,您检查它等于什么。

调用变量之前尝试设置该变量,然后使用line!=nullex:

line = reader.readLine();
if(line != null) {
    //do something
}
 类似资料:
  • 向传递的 URL 发出一个 GET 请求。 使用 XMLHttpRequest web API 向给定的 url 发出 get 请求。 通过调用给定的 callback 和 responseText 来处理 onload 事件。 通过运行提供的 err 函数,处理onerror事件。 省略第四个参数 err ,默认将错误记录到控制台的 error 流。 const httpGet = (url,

  • 试图在我的开发机器上启动我的rails服务器给了我这个错误: 查看服务器日志,我发现它正在查找icu4c版本63,但我在icu4c版本64.2。按照这里的说明,我能够重新安装icu4c 63.1版。然后我可以启动我的服务器。执行psql-version告诉我我正在运行11.2。 但是,当我启动rails控制台时,当我输入命令时 我收到警告消息: 我该如何解决这个问题?

  • 我按照这个教程:https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer 现在我在这一点上: 我的问题是这条线... 显示一个错误:不兼容的类型。需要android。应用程序。FragmentManager发现:android。支持v4.app。碎片管理器。 我看到了一些帖子,但它们不适合我。 我用AppC

  • 我正在使用JPA,列类型为: 我使用PostgreSql作为我的数据库,它支持bpchar类型,现在我尝试为我的测试添加H2,但问题是,当我运行测试时,我得到以下结果: 是否有其他方法可以实现这一目标?

  • 当我部署我的项目时,我会得到这个错误代码 SLF4J:详见http://www.slf4j.org/codes.html#version_mismatch。AVR.ApplicationContext日志信息:初始化spring根WebApplicationContext AVR。30,2014 10:16:37 AM org.apache.catalina.core.StandardContex

  • 我定义jackoson序列化器并将其添加到java类中,如下所示: 编译器出现以下错误: 注释的定义为: 如果我从ReportFilterDeserializer中删除泛型attibute,它将通过编译。我不明白编辑为什么抱怨。