How to get information from a website using HTTP and XML or SOAP protocol

Posted on 2004-11-17
Medium Priority
Last Modified: 2010-03-31
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.ActionListener;
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,

     private JButton buttonWeather, buttonExit;
     DataInputStream in = null;
     private boolean getWeather = false;

     public Client() {
          Container container = getContentPane();
          container.setLayout(new FlowLayout());

          labelZipcode = new JLabel("ZipCode ");
          textZipcode = new JTextField(15);

          buttonWeather = new JButton("     Get Weather Info     ");

          labelsky = new JLabel("Sky ");
          textsky = new JTextField(15);

          labeltemp = new JLabel("Temp ");
          texttemp = new JTextField(15);

          labelhumidity = new JLabel("Humidity ");
          texthumidity = new JTextField(15);

          labelwindspeed = new JLabel("Wind speed ");
          textwindspeed = new JTextField(15);

          labelbarometer = new JLabel("Barometer ");
          textbarometer = new JTextField(15);

          labeldew = new JLabel("Dew ");
          textdew = new JTextField(15);

          labelheat = new JLabel("Heat ");
          textheat = new JTextField(15);

          labelvisibility = new JLabel("Visibillity ");
          textvisibility = new JTextField(15);

            buttonExit = new JButton(" Exit");

          setSize(200, 600);

          // Event handler for handing the button events
          ButtonHandler handler = new ButtonHandler();


     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("Received "+t1+" "+t2+" "+t3+" "+t4);
               if (t1 > 20 && t2 < 40){

                    System.out.println("in 1");
                    textsky.setText("Sky Condition: " + "Clear");
               if (t3 > 40 && t4 < 80) {
                              .setText("Sky Condition: "
                                        + "Partly Cloudy");
                    System.out.println("in 2");
                    textsky.setText("Sky Condition: " + "Cloudy");
                    System.out.println("in 3");
               texttemp.setText("Temperature: " + in.readInt()+"\n");
               System.out.println("In last");
               texthumidity.setText("Humidity: " + in.readInt());
               textwindspeed.setText("Wind Speed: " + in.readInt());
               textbarometer.setText("Barometer: " + in.readInt());
               textdew.setText("Dewpoint: " + in.readInt());
               textheat.setText("Heat Index: " + in.readInt());
               textvisibility.setText("Visibility: " + in.readInt());
               try {
               } catch (InterruptedException e) {
                    // TODO Auto-generated catch block

     // Button Click Event Handler
     class ButtonHandler implements ActionListener {
          public void actionPerformed(ActionEvent 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.getOutputStream());
                         //sending the message to server
                         out.print("Hello from client\n");
                         //reading the response using input stream
                         in = new DataInputStream(client

                         //closing the streams
                         getWeather = true;

                        catch (Exception err)
                         System.err.println("* err" + err);
                        if(event.getSource()== buttonExit)



     public static void main(String a[]) {
          Client abc = new Client();
          try {
          } catch (IOException e) {
               // TODO Auto-generated catch block


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(labelConnection );
           textConnection = new JTextField(15) ;

          setSize ( 200,400) ;

               //creating server socket binding at port # 3000
               ServerSocket server=new ServerSocket(3000);
               System.out.println("Server binded at "+((server.getInetAddress()).getLocalHost()).getHostAddress()+":3000");
               System.out.println("Run the Client");
               //ready to accept client request
               Socket socket=server.accept();

               //opening the input stream to read data from client connection
               BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
               //using output stream responsing data
               DataOutputStream out=new DataOutputStream(socket.getOutputStream());
               while (true)
                  for (int i=0; i<8; i++)
                      int s =(int)(Math.random()*10);
               //closing the in & out streams
            //   in.close();
          catch(Exception err)
               System.err.println("* err"+err);


     public static void main(String a[])
          new Server();


Question by:prashanth_gurijala
1 Comment

Accepted Solution

jprgn earned 2000 total points
ID: 12612249
check this link it will give you complete idea of using webservice with soap via java


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question