Example Using Weather Underground for Android parsing XML

Small_nono_04Here is an example of requesting a XML document from Weather Underground (http://www.wunderground.com/weather/api/) to get the local weather.  The example also shows how to parse the XML document for Android (Java) after it is returned to display the weather.

Result:device-2014-03-24-151318

URL:

http://api.wunderground.com/api/51cda8abeca78e10/conditions/q/37.776289,-122.395234.xml

Activity:

 

package com.nathanhaze.weather;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

String temperature;
Bitmap icon = null;
TextView tempText;

ProgressDialog dialog;
String temp;

private XPath xPath = XPathFactory.newInstance().newXPath();

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	tempText = (TextView) findViewById(R.id.tempText);

new retrieve_weatherTask().execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}

protected class retrieve_weatherTask extends AsyncTask<Void, String, String> {

	protected void onPreExecute(){
		dialog = new ProgressDialog(MainActivity.this);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.setMessage("Loading…");
		dialog.setCancelable(false);
		dialog.show();
	}

	@Override
	protected String doInBackground(Void ...arg0) {
	// TODO Auto-generated method stub
	String qResult = "";
	HttpClient httpClient = new DefaultHttpClient();
	HttpContext localContext = new BasicHttpContext();
	HttpGet httpGet = new HttpGet("http://api.wunderground.com/api/51cda8abeca78e10/conditions/q/37.776289,-122.395234.xml");

	try {
	HttpResponse response = httpClient.execute(httpGet,
	localContext);
	HttpEntity entity = response.getEntity();

	if (entity != null) {
	InputStream inputStream = entity.getContent();
	Reader in = new InputStreamReader(inputStream);
	BufferedReader bufferedreader = new BufferedReader(in);
	StringBuilder stringBuilder = new StringBuilder();
	String stringReadLine = null;
	while ((stringReadLine = bufferedreader.readLine()) != null) {
	stringBuilder.append(stringReadLine + "\n");
	}
	qResult = stringBuilder.toString();
	}

	} catch (ClientProtocolException e) {
	e.printStackTrace();
	} catch (IOException e) {
	e.printStackTrace();
	}

	Document dest = null;
	DocumentBuilderFactory dbFactory = DocumentBuilderFactory
	.newInstance();
	DocumentBuilder parser;
	try {
	parser = dbFactory.newDocumentBuilder();
	dest = parser
	.parse(new ByteArrayInputStream(qResult.getBytes()));
	} catch (ParserConfigurationException e1) {
	e1.printStackTrace();
	} catch (SAXException e) {
	e.printStackTrace();
	} catch (IOException e) {
	e.printStackTrace();
	}

	temp = sv ("//current_observation/temperature_string", dest);

	Log.d("TEMP", temp);

	return temp;

	}

	public String sv(String query, Node node) {

		String rs = "";

		try {
			Node n = (Node) xPath.evaluate(query, node, XPathConstants.NODE);
			if (n != null) {
				rs = n.getTextContent();
			}
		} catch (Exception e) {
			rs = "";
		}
		return rs;
	}

	protected void onPostExecute(String result) {
		if(dialog.isShowing()){
			dialog.dismiss();
			tempText.setText("Temperature: "+ temp);
	  }
    }

  }
}

Layout:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
 >

<TextView
android:id="@+id/tempText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dateText"
android:layout_toRightOf="@+id/icon"
android:textColor="#333333"

/>

</RelativeLayout>

Manifest Permissions:

<uses-permission android:name=”android.permission.INTERNET” />


Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys