Hi My android project is
- A GPSTracker.java class which gives Latitude and longitude
- A RemoteFetch.java class which get the JSON from an adress API is in static state
A MainActivity.java but the url to get the json is like that :
private static final String OPEN_WEATHER_MAP_API = "http://api.openweathermap.org/data/2.5/weather?" + "q=%s&" +"lat="+"&lon=" + "&units=metric";
My latitude and longitude are in the GPSTracker, but when I'm getting them, the it don't let me get these values as they're not in static format...
So as soon as I'm adding them in my openweatherapi url I have the error :
"Non static field cannot be referenced from a static context" .
Is there a way to "cast" a string/int format to a static format ??
If you need more info just say it to me please.
here some code
private void updateWeatherData(final String city){
new Thread(){
public void run(){
final JSONObject json = RemoteFetch.getJSON(MainActivity.this,city);
if(json == null){
handler.post(new Runnable(){
public void run(){
Toast.makeText(MainActivity.this.getApplicationContext(),R.string.place_not_found,Toast.LENGTH_LONG).show();
}
});
} else {
handler.post(new Runnable(){
public void run(){
renderWeather(json);
}
});
}
}
}.start();
}
in RemoteFetch.java
private static final String OPEN_WEATHER_MAP_API =
"http://api.openweathermap.org/data/2.5/weather?" + "q=%s&" +"lat="+"&lon=" + "&units=metric";
public static JSONObject getJSON(Context context, String city){
try {
// gps.getLatitude();
URL url = new URL(String.format(OPEN_WEATHER_MAP_API, city));
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();
connection.addRequestProperty("x-api-key",
context.getString(R.string.open_weather_maps_app_id));
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
StringBuffer json = new StringBuffer(1024);
String tmp="";
while((tmp=reader.readLine())!=null)
json.append(tmp).append("\n");
reader.close();
JSONObject data = new JSONObject(json.toString());