【问题标题】:Basic Android Application to get the current Temperature of a city that user passes获取用户通过的城市的当前温度的基本 Android 应用程序
【发布时间】:2012-02-10 10:52:04
【问题描述】:

我昨天刚刚开始开发 android 应用程序,并且对这项技术非常感兴趣。

我需要创建一个应用程序,用户需要在edittext中输入城市名称,然后该城市的当前温度显示在textview中。

我想使用以下 URL 来轻松添加城市名称。 目前我在这里路过悉尼。

http://www.google.com/ig/api?weather=sydney

请帮我解决这个问题。

【问题讨论】:

  • 你知道webservice调用吗?即 HTTP 调用?
  • 我在您的问题中找不到任何“满意的问题”...
  • 这是你的错 selvin.. 缺乏头脑

标签: java android google-maps-api-3 android-2.2-froyo


【解决方案1】:

我猜你想知道如何使用通过调用 url 得到的 XML 文档?!

DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(http://www.google.com/ig/api?weather=sydney); httpget.setHeader("Content-Type", "application/xml"); ResponseHandler responseHandler = new BasicResponseHandler(); 字符串 responseBody = httpclient.execute(httpget, responseHandler);

这基本上就是您所需要的。响应正文应包含 xml 文档。在那里你应该能够得到你的体温。

您可以通过将凭据和其他参数添加到 HttpClient 构造函数来改进它。参见例如http://massapi.com/class/ba/BasicHttpParams.html

【讨论】:

    【解决方案2】:

    对该调用的响应返回一个 XML。您需要在 DOM 和 SAX 之间做出决定来解析 xml 响应。从 google 开始对 android xml 解析。我找到了这个链接,还有更多。一切顺利。

    http://android-pro.blogspot.in/2011/07/parsing-xml-wit-dom-parser.html

    【讨论】:

      【解决方案3】:

      使用 SAX 解析器,您可以执行以下操作。

      /* 将空白替换为 HTML 等效项。 */ //url = new URL(queryString.replace(" ", "%20"));

                           /* Get a SAXParser from the SAXPArserFactory. */
                           SAXParserFactory spf = SAXParserFactory.newInstance();
                           SAXParser sp = spf.newSAXParser();
      
                           /* Get the XMLReader of the SAXParser we created. */
                           XMLReader xr = sp.getXMLReader();
      
                           /*
                            * Create a new ContentHandler and apply it to the
                            * XML-Reader
                            */
                           GoogleWeatherHandler gwh = new GoogleWeatherHandler();
                           xr.setContentHandler(gwh);
      
                           /* Use HTTPClient to deal with the URL */ 
                           HttpClient httpclient = new DefaultHttpClient(); 
                           HttpGet httpget = new HttpGet(queryString.replace(" ", "%20")); 
                           Log.d(DEBUG_TAG, "executing request " + httpget.getURI()); 
                           // create a response handler 
                           ResponseHandler<String> responseHandler = new BasicResponseHandler();
                           Log.i("Respond Handler","Step 1");
                           String responseBody = httpclient.execute(httpget, responseHandler); 
                            Log.d(DEBUG_TAG, "response from httpclient:\n "+responseBody); 
      
                           ByteArrayInputStream is = new ByteArrayInputStream(responseBody.getBytes()); 
                           xr.parse(new InputSource(is)); 
                           Log.d(DEBUG_TAG, "parse complete"); 
                           // parse complete
      

      【讨论】:

      • 是的,这是可能的;我会在某处上传并在此处提供网址。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2013-04-10
      • 1970-01-01
      相关资源
      最近更新 更多