从一个活动到另一个活动的值

时间:2012-12-11 15:38:48

标签: java android class outer-join

我需要从一个班级Strings获得LocationGetter.java

      @Override
  public void onLocationChanged(Location location) {
    int latitute = (int) (location.getLatitude());
    int longitude = (int) (location.getLongitude());
    String lat = (String.valueOf(latitute));
    String lon = (String.valueOf(longitude));

    //latituteField.setText(String.valueOf(lat));
    //longitudeField.setText(String.valueOf(lng));
  }

到另一个班级ParseJSON.java

public String readWeatherFeed() {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://free.worldweatheronline.com/feed/weather.ashx?key=46d1ef574c094426121012&format=json&q="+lat+","+lon);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e(ParseJSON.class.toString(), "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();

}

所以我想使用第二类URL字符串中第一个类的String lat,lon值。

提前致谢。

3 个答案:

答案 0 :(得分:0)

;)你需要使用intent和BroadcastReceiver才能知道下载完成并使用AsyncTask下载JSON

你有很多工作

答案 1 :(得分:0)

您可以通过在LocationGetter类中创建String lat, lon类的Object,将ParseJSON从LocationGetter类传递到ParseJSON类:

     @Override
  public void onLocationChanged(Location location) {
    int latitute = (int) (location.getLatitude());
    int longitude = (int) (location.getLongitude());
    String lat = (String.valueOf(latitute));
    String lon = (String.valueOf(longitude));
    ParseJSON parsjosnobj=new ParseJSON();
    String strresutn =parsjosnobj.readWeatherFeed(lat,lon)
    //latituteField.setText(String.valueOf(lat));
    //longitudeField.setText(String.valueOf(lng));
  }

并在ParseJSON课程中将readWeatherFeed参数化为:

public String readWeatherFeed(String lat,String lon) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://free.worldweatheronline.com
   /feed/weather.ashx?key=46d1ef574c094426121012&format=json&q="+lat+","+lon);
    try {
   //your code here

注意:此解决方案仅在您的ParseJSON类不是活动时才有效。如果ParseJSON是一个活动,则使用Intent将数据从一个活动传递到其他< / p>

答案 2 :(得分:0)

如果这些活动涉及多个活动,您可以通过intent extras传递它们。在第一个类中,当您定义Intent以启动第二个Activity时,您需要添加一个额外的。

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("lat", mLatitude);
intent.putExtra("lon", mLongitude);
startActivity(intent);

然后在第二个活动中,您可以通过getExtras获取这些内容。

Bundle extras = getIntent().getExtras();
float latitude = extras.getFloat("lat");
float longitude = extras.getFloat("lon");