0

首先,我只在 Android/Java 中编码了几个星期,所以如果这是一个简单的问题或者我不明白你的答案,我深表歉意。

我正在尝试从手机中获取纬度和经度数据,然后在以下日出/日落网络服务中使用它: http ://www.earthtools.org/webservices.htm

我设法设置了一个按钮在文本视图中显示纬度和经度,并且我设置了另一个按钮以使用静态 url 显示 web 服务的结果。现在我只需要将两者结合起来。

如何获取纬度和经度数据并将其放入 webservive 的 url?( http://www.earthtools.org/sun/LATITUDE/LONGITUDE/28/03/99/0 ) _

这是我的代码当前的样子:

    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<Void, Void, String> {

    protected String getASCIIContentFromEntity(HttpEntity entity)
            throws IllegalStateException, IOException {
        InputStream in = entity.getContent();
        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n > 0) {
            byte[] b = new byte[4096];
            n = in.read(b);
            if (n > 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
    }
}
}

我尝试使用以下代码,但无法使其工作:

    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);

任何帮助将不胜感激,因为我已经坚持了几天了。

谢谢 =)

4

2 回答 2

1

这将为您提供月份和日期。

Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());

SimpleDateFormat df = new SimpleDateFormat("dd/MM");
String formattedDate = df.format(c.getTime());

现在你可以这样做:

String finalURL = "http://www.earthtools.org/sun/"+LatCoord.getText().toString().trim()+"/"+LongCoord.getText().toString().trim()+"/"+formattedDate+"/99/0";

现在将这个字符串传递给HttpGet这样的:

HttpGet httpGet = new HttpGet(finalURL);
于 2013-03-29T12:09:33.420 回答
0

也许您应该将 url 字符串中的“,”更改为“/”。像这样:

String url = "http://www.earthtools.org/sun/" + String.valueOf(location.getLatitude()) + "/" + String.valueOf(location.getLongitude()) + "/28/03/99/0"; 

因为对 web 服务的调用是 LATITUDE/LONGITUDE/ 而不是 LATITUDE,LONGITUDE/

于 2013-03-29T11:59:36.907 回答