Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing latitude and longitude to find sunrise/sunset times via REST webservice
    text
    copied!<p>Firstly I've only been coding in Android/Java for a couple weeks so apologies if this is a simple question or if I don't understand your answer.</p> <p>I'm trying to take the latitude and longitude data from the phone and then use this in the following sunrise/sunset webservice: <a href="http://www.earthtools.org/webservices.htm" rel="nofollow">http://www.earthtools.org/webservices.htm</a></p> <p>I have managed to set up a button display the latitude and longitude in a textview and I have set another button up to display the result of the webservice using a static url. Now I just need to combine the two.</p> <p>How do I go about taking the latitude and longitude data and putting into the url of the webservive? (<a href="http://www.earthtools.org/sun/" rel="nofollow">http://www.earthtools.org/sun/</a><strong>LATITUDE/LONGITUDE</strong>/28/03/99/0)</p> <p>Here is how my code currently looks:</p> <pre><code> package richgrundy.learnphotography; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import android.os.AsyncTask; import android.widget.EditText; public class SunriseSunset extends Activity implements OnClickListener { public Button getLocation; public TextView LongCoord; public TextView LatCoord; public double longitude; public double latitude; public LocationManager lm; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sunrisesunset); findViewById(R.id.my_button).setOnClickListener(this); lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener()); getLocation = (Button) findViewById(R.id.GetLocation); LongCoord = (TextView) findViewById(R.id.LongCoord); LatCoord = (TextView) findViewById(R.id.LatCoord); getLocation.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { showCurrentLocation(); } }); } protected void showCurrentLocation() { // TODO Auto-generated method stub Location location = lm .getLastKnownLocation(LocationManager.GPS_PROVIDER); latitude = location.getLatitude(); longitude = location.getLongitude(); LongCoord.setText(Double.toString(longitude)); LatCoord.setText(Double.toString(latitude)); } @Override public void onClick(View arg0) { Button b = (Button) findViewById(R.id.my_button); b.setClickable(false); new LongRunningGetIO().execute(); } private class LongRunningGetIO extends AsyncTask&lt;Void, Void, String&gt; { protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { InputStream in = entity.getContent(); StringBuffer out = new StringBuffer(); int n = 1; while (n &gt; 0) { byte[] b = new byte[4096]; n = in.read(b); if (n &gt; 0) out.append(new String(b, 0, n)); } return out.toString(); } @Override protected String doInBackground(Void... params) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet( "http://www.earthtools.org/sun/52.77431872/-1.20639/28/03/99/0"); String text = null; try { HttpResponse response = httpClient.execute(httpGet, localContext); HttpEntity entity = response.getEntity(); text = getASCIIContentFromEntity(entity); } catch (Exception e) { return e.getLocalizedMessage(); } return text; } protected void onPostExecute(String results) { if (results != null) { EditText et = (EditText) findViewById(R.id.my_edit); et.setText(results); } Button b = (Button) findViewById(R.id.my_button); b.setClickable(true); } } class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } } </code></pre> <p>I tried using the following code but couldn't get that to work:</p> <pre><code> HttpClient httpClient = new DefaultHttpClient(httpParameters); HttpContext localContext = new BasicHttpContext(); String url = "http://www.earthtools.org/sun/" + String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude()) + "/28/03/99/0"; HttpGet httpGet = new HttpGet(url); </code></pre> <p>Any help would be massively appreciated as I've been stuck on this for a couple days now.</p> <p>Thanks =)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload