2016-04-21 12 views
0

Ich versuche, eine App mit Forecast API zu entwickeln. Aber diese API erlaubt nur, Daten von nur einer Stadt zu erhalten, um eine Breite und eine Länge zu erhalten. Das ist mein CodeErhalten Sie viele Städte mit Forecast API

final double latitude=21.0260; 
final double longtitude=105.8471; 
String forecastUrl="https://api.forecast.io/forecast/"+apiKey+"/"+latitude+","+longtitude; 

.Dieser Code wird Daten aus nur Hanoi Stadt zurückgeben. Wie man Daten von 2 oder mehr Städten erhält, indem man nur diese Latidude und Longitude benutzt?
Dies ist meine Funktion genannt, aber ich weiß nicht, wie zu-Schleife zu ändern, weil GetForecast iam() aufrufen Funktion:

final double[] latitudes={21.0260,12.0260,21.1260}; 
    final double[] longtitudes={23.0260,42.0260,51.1260}; 

    mRefreshImageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getForecast(latitudes,longtitudes); 
     } 
    }); 
    getForecast(latitudes,longtitudes); 
Log.d(TAG, "MainUI code is running"); 
} 

private void getForecast(double[] latitude,double[] longtitude) { 
    String apiKey="a424ddd79338808311d0a6197a12731e"; 
    String forecastUrl="https://api.forecast.io/forecast/"+apiKey+"/"+latitude+","+longtitude; 

    if (isNetWorkAvailable()) { 
     toogleRefresh(); 
     OkHttpClient client = new OkHttpClient(); 
     Request req = new Request.Builder() 
       .url(forecastUrl) 
       .build(); 
     Call call = client.newCall(req); 
     call.enqueue(new Callback() { 
      @Override 
      public void onFailure(Call call, IOException e) { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         toogleRefresh(); 
        } 
       }); alertUser(); 
      } 
      @Override 
      public void onResponse(Call call, Response response) throws IOException { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         toogleRefresh(); 
        } 
       }); 
       try { 
        String jsonData=response.body().string(); 
        Log.v(TAG, jsonData); 
        if (response.isSuccessful()) { 
         mCurrentWeather=getCurrentDetail(jsonData); 
         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           updateDisplay(); 
          } 
         }); 

        } else { 
         alertUser(); 
        } 
       } catch (IOException e) { 
        Log.e(TAG, "Exception caught:", e); 
       }catch (JSONException e){ 
        Log.e(TAG, "Exception caught:", e); 
       } 
      } 
     }); 
    }else { 
     Toast.makeText(this, R.string.error_network_unavailable,Toast.LENGTH_LONG).show(); 
    } 
} 


updateDetail:

private void updateDisplay() { 
    mTemperatureLabel.setText(mCurrentWeather.getTemperature() + ""); 
    mHumidityValue.setText(mCurrentWeather.getHumidity() + ""); 
    mTimeLabel.setText("At "+ mCurrentWeather.getFormattedTime()+" it will be"); 
    mPrecipeValue.setText(mCurrentWeather.getPrecipChance()+""); 
    mSummaryValue.setText(mCurrentWeather.getSumary()); 
    Drawable drawable=getResources().getDrawable(mCurrentWeather.getIconId()); 
    mIconImageView.setImageDrawable(drawable); 
} 

getCurrentDetails():

private CurrentWeather getCurrentDetail(String jsonData) throws JSONException{ 
    JSONObject forecast= new JSONObject(jsonData); 
    String timeZone=forecast.getString("timezone"); 
    Log.i(TAG,"From Json" + timeZone); 
    JSONObject currently=forecast.getJSONObject("currently"); 
    CurrentWeather currentWeather= new CurrentWeather(); 
    currentWeather.setHumidity(currently.getDouble("humidity")); 
    currentWeather.setTime(currently.getLong("time")); 
    currentWeather.setIcon(currently.getString("icon")); 
    currentWeather.setPrecipChance(currently.getDouble("precipProbability")); 
    currentWeather.setSumary(currently.getString("summary")); 
    currentWeather.setTemperature(currently.getDouble("temperature")) ; 
    currentWeather.setTimeZone(timeZone); 
    Log.d(TAG,currentWeather.getFormattedTime()); 
    return currentWeather; 
} 

Antwort

0

Machen Sie Array von Breiten und Längen und holen Sie dann Url mit For-Schleife.

zB: -

final double[] latitudes={21.0260,12.0260,21.1260}; 
final double[] longitudes={23.0260,42.0260,51.1260}; 

String[] forecastUrls; 

for(int i;i<latitudes.lenght;i++){ 

    forecastUrls[i] = "https://api.forecast.io/forecast/"+apiKey+"/"+latitudes[i]+","+longtitude[i]; 
} 

Aktualisiert: -

mRefreshImageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      for(int i;i<latitudes.length;i++){ 
       getForecast(latitudes[i],longtitudes[i]); 
      } 
     } 
    }); 
+0

Danach Wie kann ich erste Stadt nennen und zweite Stadt auf zwei verschiedene Textview1 und Textview2? Können Sie mir das deutlicher vorschlagen? –

+0

Wie rufst du ohne Array (in Frage)? –

+0

'OkHttpClient client = neu OkHttpClient();
Anfrage req = new Request.Builder() .url (prognoseUrl) .build();
Call call = client.newCall (req); '
ich rufe so –

Verwandte Themen