你好我的Android项目是获取价值
- 一个GPSTracker.java类这给纬度和经度
- ,其从ADRESS API获取JSON一个RemoteFetch.java类在静止状态
一个MainActivity.java 但是URL获得JSON是这样的:
私有静态最后弦乐OPEN_WEATHER_MAP_API = “http://api.openweathermap.org/data/2.5/weather?“ +“q =%s &”+“lat =”+“& lon =”+“& units = metric”;
我的经度和纬度都在GPSTracker,但是当我得到他们来说,这不要让他们在静态格式不是我把这些价值观......
所以只要我将它们添加到我的openweatherapi url中,我有错误:
“非静态字段不能从静态上下文中引用”。
有没有办法将一个字符串/ int格式“转换”为静态格式?
如果您需要更多信息,请直接告诉我。
这里的一些代码
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();
}
在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());
你可以改变你的GPSTracker类获取坐标方法是静态的吗? – Xcihnegn 2015-02-06 10:28:55