Android Open Source - spotthestation Http Polling Thread






From Project

Back to project page spotthestation.

License

The source code is released under:

Apache License

If you think the Android project spotthestation listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package Threads;
//  w  w w  .j  ava2s .  c  om
import Utilities.ZoneCalculator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JLabel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author xxc9071
 */
public class HttpPollingThread implements ActionListener
{

    private static final String ISS_URL = "http://api.open-notify.org/iss-now/v1/";
    private static final String NASA_CONTROL_SERVER = "http://matai:8080/NASAControlServer/"
            + "resources/notification/notify/";
    private JLabel statusLabel;

    public HttpPollingThread(JLabel statusLabel)
    {
        this.statusLabel = statusLabel;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        URL url;
        HttpURLConnection connection = null;
        try
        {
            // Obtain location of the ISS
            url = new URL(ISS_URL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // Send the connection
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String response = rd.readLine();

            String latitude = response.substring(response.indexOf("latitude") + 11);
            latitude = latitude.substring(0, latitude.indexOf(","));
            double latitudeDouble = Double.parseDouble(latitude);

            String longitude = response.substring(response.indexOf("longitude") + 12);
            longitude = longitude.substring(0, longitude.indexOf("}"));
            double longitudeDouble = Double.parseDouble(longitude);
            rd.close();
            connection.disconnect();


            int issZone = ZoneCalculator.calculateZone(longitudeDouble, latitudeDouble);

            // Upload data to our server !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            url = new URL(NASA_CONTROL_SERVER + 49);
            System.out.println("url: " + url);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // Send the connection
            is = connection.getInputStream();


            // Update status
            statusLabel.setText("ISS last location: " + latitude + ", " + longitude);

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if (connection != null)
            {
                connection.disconnect();
            }
        }
    }
}




Java Source Code List

Entities.NASAImageCommentTable.java
Entities.NASAImageCommentTable_.java
Entities.NASAImageTable.java
Entities.NASAImageTable_.java
Entities.NASARegistrationTable.java
Entities.NASARegistrationTable_.java
.ISSPollingApp.java
.NotificationMDB.java
Servlets.ImageDownloadServlet.java
Servlets.ImageUploadServlet.java
Servlets.MarkerDownloadServlet.java
Servlets.RegistrationServlet.java
Threads.HttpPollingThread.java
Utilities.ZoneCalculator.java
Utilities.ZoneCalculator.java
WebServices.NotificationService.java
nasa.android.spotthestation.CameraActivity.java
nasa.android.spotthestation.CameraPlusActivity.java
nasa.android.spotthestation.GCMIntentService.java
nasa.android.spotthestation.HelpActivity.java
nasa.android.spotthestation.ImageViewActivity.java
nasa.android.spotthestation.MapActivity.java
nasa.android.spotthestation.MyCompassView.java
nasa.android.spotthestation.TwitterActivity.java
nasa.android.spotthestation.TwitterWebviewActivity.java
nasa.android.spotthestation.UploadToNasaServerActivity.java
org.netbeans.rest.application.config.ApplicationConfig.java