2012-12-11 27 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");