2012-12-11 25 views
-1

我需要从一个类LocationGetter.java得到Strings值到另一个

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

} 

所以我想纬度使用的值的字符串,LON从第二个类的URL字符串中的第一个类。

在此先感谢。

回答

0

;),你需要使用意图和广播接收器知道下载完成,使用的AsyncTask的下载JSON

你有很多工作

0

,你可以通过创建一个从LocationGetter类通过String lat, lon到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是一种活动,然后使用意向从一个活动的数据传递到其他

0

如果这些跨多个活动,这个解决方案,只有当你的ParseJSON类不是一个活动工作,你可以通过意向演员传递他们。在您的第一堂课中,当您将意图定义为启动第二个活动时,您将额外添加一个。

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