prashanth_gurijala
asked on
How to get information from a website using HTTP and XML or SOAP protocol
Here is my code for Client/Server application where upon connecting the server sends the weather information to client window.But, Can any one make changes so that client application will connect to National weather service website (http://weather.gov/xml) using SOAP or HTTP and XML and display weather conditions.Here my code accpets a zip code but, in the new application I want to give longitute and latitude as inputs to retrieve the weather information and in my code values are refreshed for 5 sec. I want to add a manual refresh button. So, that when refreshed the values are refreshed in the client window. And also in the client window it should display that XML or SOAP is used in the retrieving process.The four variables in display on client side may be(Max Temperature, Min Temperature,Cloud cover ammount, 12 Hr probability of precipitation, Dewpoint, Wind speed, Wind direction, Wave height).Can anybody make changes to my code. So, that the application runs according to the specification.I would really appreciate, if any one can help me with this.
Client Processes...
....................
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Client extends JFrame
{
private JTextField textZipcode, textsky, texttemp, texthumidity,
textwindspeed, textbarometer, textdew, textheat, textvisibility;
private JLabel labelZipcode, labelsky, labeltemp, labelhumidity,
labelwindspeed, labelbarometer, labeldew, labelheat,
labelvisibility;
private JButton buttonWeather, buttonExit;
DataInputStream in = null;
private boolean getWeather = false;
public Client() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
labelZipcode = new JLabel("ZipCode ");
container.add(labelZipcode );
textZipcode = new JTextField(15);
container.add(textZipcode) ;
buttonWeather = new JButton(" Get Weather Info ");
container.add(buttonWeathe r);
labelsky = new JLabel("Sky ");
container.add(labelsky);
textsky = new JTextField(15);
container.add(textsky);
labeltemp = new JLabel("Temp ");
container.add(labeltemp);
texttemp = new JTextField(15);
container.add(texttemp);
labelhumidity = new JLabel("Humidity ");
container.add(labelhumidit y);
texthumidity = new JTextField(15);
container.add(texthumidity );
labelwindspeed = new JLabel("Wind speed ");
container.add(labelwindspe ed);
textwindspeed = new JTextField(15);
container.add(textwindspee d);
labelbarometer = new JLabel("Barometer ");
container.add(labelbaromet er);
textbarometer = new JTextField(15);
container.add(textbaromete r);
labeldew = new JLabel("Dew ");
container.add(labeldew);
textdew = new JTextField(15);
container.add(textdew);
labelheat = new JLabel("Heat ");
container.add(labelheat);
textheat = new JTextField(15);
container.add(textheat);
labelvisibility = new JLabel("Visibillity ");
container.add(labelvisibil ity);
textvisibility = new JTextField(15);
container.add(textvisibili ty);
buttonExit = new JButton(" Exit");
container.add(buttonExit);
setSize(200, 600);
setVisible(true);
// Event handler for handing the button events
ButtonHandler handler = new ButtonHandler();
buttonWeather.addActionLis tener(hand ler);
}
public void getWeather() throws IOException {
while (true) {
if(getWeather) {
int t1 = in.readInt();
int t2 = in.readInt();
int t3 = in.readInt();
int t4 = in.readInt();
System.out.println("Receiv ed "+t1+" "+t2+" "+t3+" "+t4);
if (t1 > 20 && t2 < 40){
System.out.println("in 1");
textsky.setText("Sky Condition: " + "Clear");
}
if (t3 > 40 && t4 < 80) {
textsky
.setText("Sky Condition: "
+ "Partly Cloudy");
System.out.println("in 2");
}
else{
textsky.setText("Sky Condition: " + "Cloudy");
System.out.println("in 3");
}
texttemp.cut();
texttemp.setText("Temperat ure: " + in.readInt()+"\n");
//texttemp.setText("Temper ature:");
System.out.println("In last");
texthumidity.setText("Humi dity: " + in.readInt());
textwindspeed.setText("Win d Speed: " + in.readInt());
textbarometer.setText("Bar ometer: " + in.readInt());
textdew.setText("Dewpoint: " + in.readInt());
textheat.setText("Heat Index: " + in.readInt());
textvisibility.setText("Vi sibility: " + in.readInt());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// Button Click Event Handler
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEven t event) {
// After pressing show Status, varios checks and the final message
// is built
if (event.getSource() == buttonWeather)
{
try {
//ceating the socket to connect to server running on same
// machine binded on port no 3000
Socket client = new Socket("localhost", 3000);
System.out.println("Client connected ");
//getting the o/p stream of that connection
PrintStream out = new PrintStream(client.getOutp utStream() );
//sending the message to server
out.print("Hello from client\n");
out.flush();
//reading the response using input stream
in = new DataInputStream(client
.getInputStream());
//System.out.println("haha .."+in.rea dInt());
//closing the streams
//in.close();
//out.close();
getWeather = true;
}
catch (Exception err)
{
System.err.println("* err" + err);
}
}
if(event.getSource()== buttonExit)
{
System.exit(1);
}
}
}
public static void main(String a[]) {
Client abc = new Client();
try {
abc.getWeather();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
.....Server.....
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame
{
private JTextField textConnection;
private JLabel labelConnection;
Container container = null;
public Server()
{
container = getContentPane() ;
container.setLayout(new FlowLayout());
labelConnection = new JLabel("Connection ") ;
container.add(labelConnect ion );
textConnection = new JTextField(15) ;
container.add(textConnecti on);
textConnection.setText("Di sconnected ");
setSize ( 200,400) ;
setVisible(true);
try
{
//creating server socket binding at port # 3000
ServerSocket server=new ServerSocket(3000);
System.out.println("Server binded at "+((server.getInetAddress( )).getLoca lHost()).g etHostAddr ess()+":30 00");
System.out.println("Run the Client");
//ready to accept client request
Socket socket=server.accept();
textConnection.cut();
textConnection.setText("Co nnected\n" );
//opening the input stream to read data from client connection
BufferedReader in= new BufferedReader(new InputStreamReader(socket.g etInputStr eam()));
System.out.println(in.read Line());
//using output stream responsing data
DataOutputStream out=new DataOutputStream(socket.ge tOutputStr eam());
while (true)
{
for (int i=0; i<8; i++)
{
int s =(int)(Math.random()*10);
out.writeInt((s*10)+20);
}
out.flush();
}
//closing the in & out streams
//out.close
// in.close();
}
catch(Exception err)
{
System.err.println("* err"+err);
textConnection.cut();
textConnection.setText("Di sconnected \n");
}
}
public static void main(String a[])
{
new Server();
}
}
Thanks,
PG
Client Processes...
....................
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Client extends JFrame
{
private JTextField textZipcode, textsky, texttemp, texthumidity,
textwindspeed, textbarometer, textdew, textheat, textvisibility;
private JLabel labelZipcode, labelsky, labeltemp, labelhumidity,
labelwindspeed, labelbarometer, labeldew, labelheat,
labelvisibility;
private JButton buttonWeather, buttonExit;
DataInputStream in = null;
private boolean getWeather = false;
public Client() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
labelZipcode = new JLabel("ZipCode ");
container.add(labelZipcode
textZipcode = new JTextField(15);
container.add(textZipcode)
buttonWeather = new JButton(" Get Weather Info ");
container.add(buttonWeathe
labelsky = new JLabel("Sky ");
container.add(labelsky);
textsky = new JTextField(15);
container.add(textsky);
labeltemp = new JLabel("Temp ");
container.add(labeltemp);
texttemp = new JTextField(15);
container.add(texttemp);
labelhumidity = new JLabel("Humidity ");
container.add(labelhumidit
texthumidity = new JTextField(15);
container.add(texthumidity
labelwindspeed = new JLabel("Wind speed ");
container.add(labelwindspe
textwindspeed = new JTextField(15);
container.add(textwindspee
labelbarometer = new JLabel("Barometer ");
container.add(labelbaromet
textbarometer = new JTextField(15);
container.add(textbaromete
labeldew = new JLabel("Dew ");
container.add(labeldew);
textdew = new JTextField(15);
container.add(textdew);
labelheat = new JLabel("Heat ");
container.add(labelheat);
textheat = new JTextField(15);
container.add(textheat);
labelvisibility = new JLabel("Visibillity ");
container.add(labelvisibil
textvisibility = new JTextField(15);
container.add(textvisibili
buttonExit = new JButton(" Exit");
container.add(buttonExit);
setSize(200, 600);
setVisible(true);
// Event handler for handing the button events
ButtonHandler handler = new ButtonHandler();
buttonWeather.addActionLis
}
public void getWeather() throws IOException {
while (true) {
if(getWeather) {
int t1 = in.readInt();
int t2 = in.readInt();
int t3 = in.readInt();
int t4 = in.readInt();
System.out.println("Receiv
if (t1 > 20 && t2 < 40){
System.out.println("in 1");
textsky.setText("Sky Condition: " + "Clear");
}
if (t3 > 40 && t4 < 80) {
textsky
.setText("Sky Condition: "
+ "Partly Cloudy");
System.out.println("in 2");
}
else{
textsky.setText("Sky Condition: " + "Cloudy");
System.out.println("in 3");
}
texttemp.cut();
texttemp.setText("Temperat
//texttemp.setText("Temper
System.out.println("In last");
texthumidity.setText("Humi
textwindspeed.setText("Win
textbarometer.setText("Bar
textdew.setText("Dewpoint:
textheat.setText("Heat Index: " + in.readInt());
textvisibility.setText("Vi
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// Button Click Event Handler
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEven
// After pressing show Status, varios checks and the final message
// is built
if (event.getSource() == buttonWeather)
{
try {
//ceating the socket to connect to server running on same
// machine binded on port no 3000
Socket client = new Socket("localhost", 3000);
System.out.println("Client
//getting the o/p stream of that connection
PrintStream out = new PrintStream(client.getOutp
//sending the message to server
out.print("Hello from client\n");
out.flush();
//reading the response using input stream
in = new DataInputStream(client
.getInputStream());
//System.out.println("haha
//closing the streams
//in.close();
//out.close();
getWeather = true;
}
catch (Exception err)
{
System.err.println("* err" + err);
}
}
if(event.getSource()== buttonExit)
{
System.exit(1);
}
}
}
public static void main(String a[]) {
Client abc = new Client();
try {
abc.getWeather();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
.....Server.....
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame
{
private JTextField textConnection;
private JLabel labelConnection;
Container container = null;
public Server()
{
container = getContentPane() ;
container.setLayout(new FlowLayout());
labelConnection = new JLabel("Connection ") ;
container.add(labelConnect
textConnection = new JTextField(15) ;
container.add(textConnecti
textConnection.setText("Di
setSize ( 200,400) ;
setVisible(true);
try
{
//creating server socket binding at port # 3000
ServerSocket server=new ServerSocket(3000);
System.out.println("Server
System.out.println("Run the Client");
//ready to accept client request
Socket socket=server.accept();
textConnection.cut();
textConnection.setText("Co
//opening the input stream to read data from client connection
BufferedReader in= new BufferedReader(new InputStreamReader(socket.g
System.out.println(in.read
//using output stream responsing data
DataOutputStream out=new DataOutputStream(socket.ge
while (true)
{
for (int i=0; i<8; i++)
{
int s =(int)(Math.random()*10);
out.writeInt((s*10)+20);
}
out.flush();
}
//closing the in & out streams
//out.close
// in.close();
}
catch(Exception err)
{
System.err.println("* err"+err);
textConnection.cut();
textConnection.setText("Di
}
}
public static void main(String a[])
{
new Server();
}
}
Thanks,
PG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.