数据未显示从Google Weather API也未显示Toast

时间:2015-03-12 18:00:57

标签: android weather-api

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

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

    }

}

HandliingStuff

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

}

2 个答案:

答案 0 :(得分:0)

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

请参阅此example

答案 1 :(得分:0)

您需要为每个按钮调用setOnClickListener。你只用一个按钮就可以调用一次。

我建议您首先使用Toast或在单击侦听器中编写长消息,以确保您实际执行侦听器。一旦程序运行,请将其取出。

法希姆也是对的。每次访问网络时,您必须在后台线程中执行此操作,否则您的用户界面将被锁定。 asyncTask is the easiest way to create the background thread you need.