0

まず、私は Android/Java でコーディングを始めて数週間しか経っていないので、これが簡単な質問であったり、あなたの答えが理解できない場合は申し訳ありません。

電話から緯度と経度のデータを取得し、これを次の日の出/日の入り Web サービスで使用しようとしています: http://www.earthtools.org/webservices.htm

テキストビューに緯度と経度を表示するボタンを設定し、静的URLを使用してWebサービスの結果を表示する別のボタンを設定しました。今、私は2つを組み合わせる必要があります。

緯度と経度のデータを取得して、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 に答える