main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/tv_cityname" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_latitude" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_longitude" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_currenttime" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_sunrise" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_sunset" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_moonrise" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_moonset" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_unit" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_value" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_dist" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="Click to Parse" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/tv_cityname" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_latitude" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_longitude" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_currenttime" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_sunrise" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_sunset" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_moonrise" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_moonset" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_unit" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_value" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_dist" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="Click to Parse" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Manifest.xml
Give internet permission...
WhetherTEMP.java
package com.WhetherTEMP;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.WhetherTEMP.ClientMethod.RequestMethod;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class WhetherTEMP extends Activity {
/** Called when the activity is first created. */
TextView cityname,latitude,dirctn,currenttime,MetricSpeed,Stemp,Ctemp,LstUpdt,unit,value,dist;
WhetherList loginsList = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cityname =(TextView)findViewById(R.id.tv_cityname);
latitude=(TextView)findViewById(R.id.tv_latitude);
dirctn=(TextView)findViewById(R.id.tv_longitude);
currenttime=(TextView)findViewById(R.id.tv_currenttime);
MetricSpeed=(TextView)findViewById(R.id.tv_sunrise);
Stemp=(TextView)findViewById(R.id.tv_sunset);
Ctemp=(TextView)findViewById(R.id.tv_moonrise);
LstUpdt=(TextView)findViewById(R.id.tv_moonset);
unit=(TextView)findViewById(R.id.tv_unit);
value=(TextView)findViewById(R.id.tv_value);
dist=(TextView)findViewById(R.id.tv_dist);
Button ok=(Button)findViewById(R.id.button1);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkLogin();
}
});
}
private void checkLogin() {
ClientMethod client = new ClientMethod("http://www.wunderground.com/cgi-bin/findweather/getForecast?brand=vitebXML&query=rajkot,india");
try {
client.Execute(RequestMethod.POST);
InputSource inSource = client.getinpSource();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
WhetherXMLHandler loginXMLhl = new WhetherXMLHandler();
xr.setContentHandler(loginXMLhl);
xr.parse(inSource);
loginsList = loginXMLhl.getLoginsList();
Message myMessage = new Message();
myMessage.obj = "";
handler2.sendMessage(myMessage);
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
private Handler handler2 = new Handler() {
@Override
public void handleMessage(Message msg) {
String lt = Double.toString(loginsList.getLati()); // CONVERT DOUBLE TO STRING
cityname.setText("City name :: " + loginsList.getCityName());
latitude.setText("Latitude :: " + lt);
currenttime.setText("Current time :: " + loginsList.getCurrenttime());
LstUpdt.setText("Last Update time :: " + loginsList.getLstUpdttime());
Ctemp.setText("Current temp Matric :: "+loginsList.getCtmpM());
Stemp.setText("Current temp Std :: " + loginsList.getCtmpS());
MetricSpeed.setText("Metric Speed :: " + loginsList.getSpeed());
dirctn.setText("Metric Direction :: " + loginsList.getDir());
unit.setText("Metric Unit :: " + loginsList.getUnit());
value.setText("Metric Value :: " + loginsList.getVal());
dist.setText("Metric Dist :: " + loginsList.getDist());
}
};
}
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.WhetherTEMP.ClientMethod.RequestMethod;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class WhetherTEMP extends Activity {
/** Called when the activity is first created. */
TextView cityname,latitude,dirctn,currenttime,MetricSpeed,Stemp,Ctemp,LstUpdt,unit,value,dist;
WhetherList loginsList = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cityname =(TextView)findViewById(R.id.tv_cityname);
latitude=(TextView)findViewById(R.id.tv_latitude);
dirctn=(TextView)findViewById(R.id.tv_longitude);
currenttime=(TextView)findViewById(R.id.tv_currenttime);
MetricSpeed=(TextView)findViewById(R.id.tv_sunrise);
Stemp=(TextView)findViewById(R.id.tv_sunset);
Ctemp=(TextView)findViewById(R.id.tv_moonrise);
LstUpdt=(TextView)findViewById(R.id.tv_moonset);
unit=(TextView)findViewById(R.id.tv_unit);
value=(TextView)findViewById(R.id.tv_value);
dist=(TextView)findViewById(R.id.tv_dist);
Button ok=(Button)findViewById(R.id.button1);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkLogin();
}
});
}
private void checkLogin() {
ClientMethod client = new ClientMethod("http://www.wunderground.com/cgi-bin/findweather/getForecast?brand=vitebXML&query=rajkot,india");
try {
client.Execute(RequestMethod.POST);
InputSource inSource = client.getinpSource();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
WhetherXMLHandler loginXMLhl = new WhetherXMLHandler();
xr.setContentHandler(loginXMLhl);
xr.parse(inSource);
loginsList = loginXMLhl.getLoginsList();
Message myMessage = new Message();
myMessage.obj = "";
handler2.sendMessage(myMessage);
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
private Handler handler2 = new Handler() {
@Override
public void handleMessage(Message msg) {
String lt = Double.toString(loginsList.getLati()); // CONVERT DOUBLE TO STRING
cityname.setText("City name :: " + loginsList.getCityName());
latitude.setText("Latitude :: " + lt);
currenttime.setText("Current time :: " + loginsList.getCurrenttime());
LstUpdt.setText("Last Update time :: " + loginsList.getLstUpdttime());
Ctemp.setText("Current temp Matric :: "+loginsList.getCtmpM());
Stemp.setText("Current temp Std :: " + loginsList.getCtmpS());
MetricSpeed.setText("Metric Speed :: " + loginsList.getSpeed());
dirctn.setText("Metric Direction :: " + loginsList.getDir());
unit.setText("Metric Unit :: " + loginsList.getUnit());
value.setText("Metric Value :: " + loginsList.getVal());
dist.setText("Metric Dist :: " + loginsList.getDist());
}
};
}
WhetherList.java
package com.WhetherTEMP;
public class WhetherList {
private String cityname,currenttime,lastupdatedtime, ctmpM,ctmpS,speed,dir,unit,val,dst;
private Double lati;
public String getCityName() {
return cityname;
}
public void setCityName(String cnm) {
cityname = cnm;
}
public Double getLati() {
return lati;
}
public void setLati(Double latit) {
lati = latit;
}
public String getCurrenttime() {
return currenttime;
}
public void setCurrenttime(String ctime) {
currenttime = ctime;
}
public String getLstUpdttime() {
return lastupdatedtime;
}
public void setLstUpdttime(String lstupdt) {
lastupdatedtime = lstupdt;
}
public String getCtmpM() {
return ctmpM;
}
public void setCtmpM(String ctm) {
ctmpM = ctm;
}
public String getCtmpS() {
return ctmpS;
}
public void setCtmpS(String cts) {
ctmpS = cts;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String spd) {
speed = spd;
}
public String getDir() {
return dir;
}
public void setDir(String dir1) {
dir = dir1;
}
public String getUnit() {
return unit;
}
public void setUnit(String u) {
unit = u;
}
public String getVal() {
return val;
}
public void setVal(String value) {
// TODO Auto-generated method stub
val = value;
}
public String getDist() {
return dst;
}
public void setDist(String dist) {
// TODO Auto-generated method stub
dst = dist;
}
}
public class WhetherList {
private String cityname,currenttime,lastupdatedtime, ctmpM,ctmpS,speed,dir,unit,val,dst;
private Double lati;
public String getCityName() {
return cityname;
}
public void setCityName(String cnm) {
cityname = cnm;
}
public Double getLati() {
return lati;
}
public void setLati(Double latit) {
lati = latit;
}
public String getCurrenttime() {
return currenttime;
}
public void setCurrenttime(String ctime) {
currenttime = ctime;
}
public String getLstUpdttime() {
return lastupdatedtime;
}
public void setLstUpdttime(String lstupdt) {
lastupdatedtime = lstupdt;
}
public String getCtmpM() {
return ctmpM;
}
public void setCtmpM(String ctm) {
ctmpM = ctm;
}
public String getCtmpS() {
return ctmpS;
}
public void setCtmpS(String cts) {
ctmpS = cts;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String spd) {
speed = spd;
}
public String getDir() {
return dir;
}
public void setDir(String dir1) {
dir = dir1;
}
public String getUnit() {
return unit;
}
public void setUnit(String u) {
unit = u;
}
public String getVal() {
return val;
}
public void setVal(String value) {
// TODO Auto-generated method stub
val = value;
}
public String getDist() {
return dst;
}
public void setDist(String dist) {
// TODO Auto-generated method stub
dst = dist;
}
}
WhetherXMLHandler.java
package com.WhetherTEMP;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class WhetherXMLHandler extends DefaultHandler {
private StringBuffer buffer = new StringBuffer();
public static WhetherList loginsList = null;
String currentValue = null;
public static WhetherList getLoginsList() {
return loginsList;
}
public static void setLoginsList(WhetherList loginsList) {
WhetherXMLHandler.loginsList = loginsList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
buffer.setLength(0);
if (localName.equals("weather")) {
if (loginsList == null) {
loginsList = new WhetherList();
}
}
else if (localName.equals("currenttemp")) {
String metric1 = attributes.getValue("metric");
loginsList.setCtmpM(metric1);
String standard1 = attributes.getValue("standard");
loginsList.setCtmpS(standard1);
}
else if(localName.equals("metric"))
{
String speed = attributes.getValue("speed");
loginsList.setSpeed(speed);
String dirct= attributes.getValue("direction");
loginsList.setDir(dirct);
String unt= attributes.getValue("unit");
loginsList.setUnit(unt);
String value= attributes.getValue("val");
loginsList.setVal(value);
String dist= attributes.getValue("dist");
loginsList.setDist(dist);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("cityname"))
loginsList.setCityName(buffer.toString());
else if (localName.equalsIgnoreCase("latitude"))
loginsList.setLati(Double.valueOf(buffer.toString()));
else if (localName.equalsIgnoreCase("currenttime"))
loginsList.setCurrenttime(buffer.toString());
else if (localName.equalsIgnoreCase("lastupdatedtime"))
loginsList.setLstUpdttime(buffer.toString());
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
buffer.append(ch, start, length);
}
}
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class WhetherXMLHandler extends DefaultHandler {
private StringBuffer buffer = new StringBuffer();
public static WhetherList loginsList = null;
String currentValue = null;
public static WhetherList getLoginsList() {
return loginsList;
}
public static void setLoginsList(WhetherList loginsList) {
WhetherXMLHandler.loginsList = loginsList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
buffer.setLength(0);
if (localName.equals("weather")) {
if (loginsList == null) {
loginsList = new WhetherList();
}
}
else if (localName.equals("currenttemp")) {
String metric1 = attributes.getValue("metric");
loginsList.setCtmpM(metric1);
String standard1 = attributes.getValue("standard");
loginsList.setCtmpS(standard1);
}
else if(localName.equals("metric"))
{
String speed = attributes.getValue("speed");
loginsList.setSpeed(speed);
String dirct= attributes.getValue("direction");
loginsList.setDir(dirct);
String unt= attributes.getValue("unit");
loginsList.setUnit(unt);
String value= attributes.getValue("val");
loginsList.setVal(value);
String dist= attributes.getValue("dist");
loginsList.setDist(dist);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("cityname"))
loginsList.setCityName(buffer.toString());
else if (localName.equalsIgnoreCase("latitude"))
loginsList.setLati(Double.valueOf(buffer.toString()));
else if (localName.equalsIgnoreCase("currenttime"))
loginsList.setCurrenttime(buffer.toString());
else if (localName.equalsIgnoreCase("lastupdatedtime"))
loginsList.setLstUpdttime(buffer.toString());
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
buffer.append(ch, start, length);
}
}
ClientMethod.java
package com.WhetherTEMP;
import java.net.URLEncoder;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
public class ClientMethod {
public enum RequestMethod {
GET, POST
}
private String url;
private ArrayList<NameValuePair> params;
private HttpEntity respEntity;
private InputSource inpSource;
public HttpEntity getrespEntity() {
return respEntity;
}
public InputSource getinpSource() {
return inpSource;
}
public void AddParam(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
public ClientMethod(String url) {
this.url = url;
params = new ArrayList<NameValuePair>();
}
public void Execute(RequestMethod method) throws Exception {
switch (method) {
case GET: {
// add parameters
String combinedParams = "";
if (!params.isEmpty()) {
combinedParams += "?";
for (NameValuePair p : params) {
String paramString = p.getName() + "="
+ URLEncoder.encode(p.getValue(), "UTF-8");
if (combinedParams.length() > 1) {
combinedParams += "&" + paramString;
} else {
combinedParams += paramString;
}
}
}
HttpGet request = new HttpGet(url + combinedParams);
executeRequest(request, url);
break;
}
case POST: {
HttpPost request = new HttpPost(url);
if (!params.isEmpty()) {
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
executeRequest(request, url);
}
}
}
private void executeRequest(HttpUriRequest request, String url) {
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
respEntity = httpResponse.getEntity();
inpSource = retrieveInputStream(respEntity);
// SAXParserFactory spf = SAXParserFactory.newInstance();
// SAXParser sp = spf.newSAXParser();
// XMLReader xr = sp.getXMLReader();
// MyXMLHandler myXMLHandler = new MyXMLHandler();
// xr.setContentHandler(myXMLHandler);
// xr.parse(inpSource);
} catch (Exception e) {
e.printStackTrace();
}
}
private InputSource retrieveInputStream(HttpEntity httpEntity) {
InputSource insrc = null;
try {
insrc = new InputSource(httpEntity.getContent());
} catch (Exception e) {
}
return insrc;
}
}
import java.net.URLEncoder;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
public class ClientMethod {
public enum RequestMethod {
GET, POST
}
private String url;
private ArrayList<NameValuePair> params;
private HttpEntity respEntity;
private InputSource inpSource;
public HttpEntity getrespEntity() {
return respEntity;
}
public InputSource getinpSource() {
return inpSource;
}
public void AddParam(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
public ClientMethod(String url) {
this.url = url;
params = new ArrayList<NameValuePair>();
}
public void Execute(RequestMethod method) throws Exception {
switch (method) {
case GET: {
// add parameters
String combinedParams = "";
if (!params.isEmpty()) {
combinedParams += "?";
for (NameValuePair p : params) {
String paramString = p.getName() + "="
+ URLEncoder.encode(p.getValue(), "UTF-8");
if (combinedParams.length() > 1) {
combinedParams += "&" + paramString;
} else {
combinedParams += paramString;
}
}
}
HttpGet request = new HttpGet(url + combinedParams);
executeRequest(request, url);
break;
}
case POST: {
HttpPost request = new HttpPost(url);
if (!params.isEmpty()) {
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
executeRequest(request, url);
}
}
}
private void executeRequest(HttpUriRequest request, String url) {
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
respEntity = httpResponse.getEntity();
inpSource = retrieveInputStream(respEntity);
// SAXParserFactory spf = SAXParserFactory.newInstance();
// SAXParser sp = spf.newSAXParser();
// XMLReader xr = sp.getXMLReader();
// MyXMLHandler myXMLHandler = new MyXMLHandler();
// xr.setContentHandler(myXMLHandler);
// xr.parse(inpSource);
} catch (Exception e) {
e.printStackTrace();
}
}
private InputSource retrieveInputStream(HttpEntity httpEntity) {
InputSource insrc = null;
try {
insrc = new InputSource(httpEntity.getContent());
} catch (Exception e) {
}
return insrc;
}
}
No comments:
Post a Comment