【问题标题】:Data Not showing from Google Weather API also Toast not showingGoogle Weather API 未显示数据也未显示 Toast
【发布时间】:2015-03-12 18:00:56
【问题描述】:

我一直在关注新的 boston android 系列,在 XML 解析教程中我遇到了一个问题。 数据未显示。在真实设备上单击按钮也没有显示操作。 代码是: weatherXML解析类

    package com.ss;

import java.net.URL;

import javax.xml.datatype.Duration;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.InputFilter.LengthFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class WeatherXMLParsing extends Activity implements OnClickListener {

    static final String baseURL = "http://api.openweathermap.org/data/2.5/weather?q=rajasthan,jaipur&mode=xml";
    TextView tvCity, tvState;
    EditText etCity, etState;
    Button bWeather;
    TextView tvWeather;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        tvCity = (TextView) findViewById(R.id.tvCity);
        tvState = (TextView) findViewById(R.id.tvState);
        etCity = (EditText) findViewById(R.id.etCity);
        etState = (EditText) findViewById(R.id.etState);
        bWeather = (Button) findViewById(R.id.btWeather);
        tvWeather = (TextView) findViewById(R.id.tvWeather);
        bWeather.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Toast.makeText(WeatherXMLParsing.this, "thanks for pressing",Toast.LENGTH_LONG);
        String c = etCity.getText().toString();
        String s = etState.getText().toString();
        // StringBuilder URL = new StringBuilder(baseURL);
        // URL.append(c+","+s+"&mode=xml");

        /*
         * System.out.print(fullurl +"========");
         * System.out.println(URL.toString());
         */

        try {
            java.net.URL website = new URL(baseURL);

            // getting xmlreader for parsing
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            HandlingXMLStuff doingWork = new HandlingXMLStuff();
            xr.setContentHandler(doingWork);
            xr.parse(new InputSource(website.openStream()));

            String information = doingWork.getInformation();

        } catch (Exception e) {
            tvWeather.setText("ERROR" + e.toString());
            Dialog d   = new Dialog(this);
            d.setTitle("ERROR"+e.toString());
            TextView tv = new TextView(this);
            d.setContentView(tv);
        }

    }

}

处理材料

package com.ss;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class HandlingXMLStuff extends DefaultHandler{


    XMLDataCollected info = new XMLDataCollected();

    @Override
    public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
        //super.startElement(uri, localName, qName, attributes);


        if(localName.equals("city"))
        {
            String city = attributes.getValue("name");
            info.setCity(city);

        }
        else if(localName.equals("temperature"))
        {
            String temp = attributes.getValue("value");

            info.setTemp(temp);
        }
    }

    public String getInformation() {
        return info.dataToString();
    }
}

XMLDataCollected

package com.ss;

public class XMLDataCollected {

    String temp = "";
    String city = "";

    public void setCity(String c) {
        city = c;
    }

    public void setTemp(String tempr) {
        temp = tempr;
    }

    public String dataToString() {
        return "In "+city +" the current temrature in faranhite is " +temp+" degrees";
    }

}

【问题讨论】:

  • downvotes 请评论downvotes。如果您认为这不是问题,请回答
  • 这里有很多代码,你能把它缩小到Minimal, Complete, and Verifiable example
  • @Rick .ok 让我总结一下。我从用户输入中获取城市和状态。单击按钮 WeatherXMLParsing 类并通过HandlingXMLStuff 解析它。你可以看到它有一个方法 getInformation 。我试图使用静态 xml 执行此操作,但它也不起作用。

标签: android weather-api


【解决方案1】:

我认为您需要学习有关从服务器获取数据的基本知识。使用asynctask调用api。

请参考example

【讨论】:

  • 我正在关注新波士顿的教程,并且工作正常。
  • 你能告诉我 SAX 解析器的问题在哪里
【解决方案2】:

您需要为每个按钮调用 setOnClickListener。您只需为一个按钮调用一次。

我建议您首先使用 Toast 或在您的点击监听器中写一条长消息,以确保您实际上正在执行该监听器。程序运行后将其取出。

法希姆也是正确的。任何时候你访问网络都必须在后台线程中进行,否则你的用户界面将被锁定。 asyncTask is the easiest way to create the background thread you need.

【讨论】:

  • 我只有一个按钮,我已经为它设置了。是的,我知道使用 aysn 任务。但是对于教程,我遵循的是他在没有 ayn 的情况下完成的,所以我正在尝试。跨度>
猜你喜欢
  • 2012-02-04
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2023-03-03
  • 2015-10-27
  • 1970-01-01
相关资源
最近更新 更多