Main Activity.Java
public class Main Activity extends Activity {
private String url1 = "http://api.openweathermap.org/data/2.5/weather?q=";
private String url2 = "&mode=xml";
private TextView country, temperature, humidity, pressure, city;
private EditText location;
private HandleXML obj;
Button weather_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location = (EditText) findViewById(R.id.editText1);
country = (TextView) findViewById(R.id.cunty_txt);
temperature = (TextView) findViewById(R.id.temprature_txt);
humidity = (TextView) findViewById(R.id.humidyty_txt);
pressure = (TextView) findViewById(R.id.prature_txt);
weather_btn = (Button) findViewById(R.id.button1);
city = (TextView) findViewById(R.id.City_txt);
weather_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url = location.getText().toString();
if (!url.equalsIgnoreCase("")) {
String finalUrl = url1 + url + url2;
country.setText(finalUrl);
obj = new HandleXML(finalUrl);
obj.fetchXML();
while (obj.parsingComplete)
;
country.setText(obj.getCountry());
temperature.setText(obj.getTemperature());
humidity.setText(obj.getHumidity());
pressure.setText(obj.getPressure());
city.setText(obj.getCity());
} else {
Toast.makeText(MainActivity.this, "Faild Conections", 3000).show();
country.setText("");
temperature.setText("");
humidity.setText("");
pressure.setText("");
city.setText("");
}
}
});
}
}
HandleXML_Services
package com.example.xml_parsing;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public class HandleXML_Services {
private String country = "county";
private String temperature = "temperature";
private String humidity = "humidity";
private String pressure = "pressure";
private String City = "City";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = true;
public HandleXML_Services(String url){
this.urlString = url;
}
public String getCountry(){
return country;
}
public String getTemperature(){
return temperature;
}
public String getHumidity(){
return humidity;
}
public String getCity(){
return City;
}
public String getPressure(){
return pressure;
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text=null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
if(name.equals("city")){
City = myParser.getAttributeValue(null,"name");
}
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("country")){
country = text;
}
else if(name.equals("humidity")){
humidity = myParser.getAttributeValue(null,"value");
}
else if(name.equals("pressure")){
pressure = myParser.getAttributeValue(null,"value");
}
else if(name.equals("temperature")){
temperature = myParser.getAttributeValue(null,"value");
}
else{
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES
, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
xml layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:singleLine="true"
android:hint="Enter City Name"
android:textSize="20sp" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Get Weather" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="75"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="Country:-"
android:textSize="20sp" />
<TextView
android:id="@+id/cunty_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="City:-"
android:textSize="20sp" />
<TextView
android:id="@+id/City_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="Temperature:-"
android:textSize="20sp" />
<TextView
android:id="@+id/temprature_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="Humidity:-"
android:textSize="20sp" />
<TextView
android:id="@+id/humidyty_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="Presser:-"
android:textSize="20sp" />
<TextView
android:id="@+id/prature_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
</LinearLayout>
</LinearLayout>
ScreenShots
public class Main Activity extends Activity {
private String url1 = "http://api.openweathermap.org/data/2.5/weather?q=";
private String url2 = "&mode=xml";
private TextView country, temperature, humidity, pressure, city;
private EditText location;
private HandleXML obj;
Button weather_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location = (EditText) findViewById(R.id.editText1);
country = (TextView) findViewById(R.id.cunty_txt);
temperature = (TextView) findViewById(R.id.temprature_txt);
humidity = (TextView) findViewById(R.id.humidyty_txt);
pressure = (TextView) findViewById(R.id.prature_txt);
weather_btn = (Button) findViewById(R.id.button1);
city = (TextView) findViewById(R.id.City_txt);
weather_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url = location.getText().toString();
if (!url.equalsIgnoreCase("")) {
String finalUrl = url1 + url + url2;
country.setText(finalUrl);
obj = new HandleXML(finalUrl);
obj.fetchXML();
while (obj.parsingComplete)
;
country.setText(obj.getCountry());
temperature.setText(obj.getTemperature());
humidity.setText(obj.getHumidity());
pressure.setText(obj.getPressure());
city.setText(obj.getCity());
} else {
Toast.makeText(MainActivity.this, "Faild Conections", 3000).show();
country.setText("");
temperature.setText("");
humidity.setText("");
pressure.setText("");
city.setText("");
}
}
});
}
}
HandleXML_Services
package com.example.xml_parsing;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public class HandleXML_Services {
private String country = "county";
private String temperature = "temperature";
private String humidity = "humidity";
private String pressure = "pressure";
private String City = "City";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = true;
public HandleXML_Services(String url){
this.urlString = url;
}
public String getCountry(){
return country;
}
public String getTemperature(){
return temperature;
}
public String getHumidity(){
return humidity;
}
public String getCity(){
return City;
}
public String getPressure(){
return pressure;
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text=null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
if(name.equals("city")){
City = myParser.getAttributeValue(null,"name");
}
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("country")){
country = text;
}
else if(name.equals("humidity")){
humidity = myParser.getAttributeValue(null,"value");
}
else if(name.equals("pressure")){
pressure = myParser.getAttributeValue(null,"value");
}
else if(name.equals("temperature")){
temperature = myParser.getAttributeValue(null,"value");
}
else{
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES
, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
xml layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:singleLine="true"
android:hint="Enter City Name"
android:textSize="20sp" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Get Weather" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="75"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="Country:-"
android:textSize="20sp" />
<TextView
android:id="@+id/cunty_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="City:-"
android:textSize="20sp" />
<TextView
android:id="@+id/City_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="Temperature:-"
android:textSize="20sp" />
<TextView
android:id="@+id/temprature_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="Humidity:-"
android:textSize="20sp" />
<TextView
android:id="@+id/humidyty_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:text="Presser:-"
android:textSize="20sp" />
<TextView
android:id="@+id/prature_txt"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
</LinearLayout>
</LinearLayout>
ScreenShots



No comments:
Post a Comment