对于长代码帖子感到抱歉,我不想错过任何东西(我是一个非常初学者)。 我查看了其他答案,以及我如何将这些代码放在一起(我认为我做得对),因为我运行它
原因我使用2.2模拟器(使用geo fix long lat通过telnet进入模拟器端口)因为我不知道为什么在后来的模拟器上它总是说天气不可用我猜测以后模拟器没有'支持GPS测试。
我认为这与此Weather类中的getCurrentWeather()调用有关。 请让我知道我做错了什么或指出我正确的方向。 提前谢谢!
我有
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
AndroidManifest.xml中的
我想说它在android 2.2模拟器上的功能就像一个魅力。我在以后的模拟器上获得null位置。并且在实际的手机上(当其他应用能够访问其GPS位置时,它在我朋友的手机上的空位置)。在我叔叔的电话(nexus 5)上,它只是崩溃了(因为它在手机上我不知道如何从中获取日志,因为我不知道如何连接我的电脑。)
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class Weather {
public final static String CLEAR_SKY = "clear sky";
public final static String FEW_CLOUDS = "few clouds";
public final static String SCATTERED_CLOUDS = "scattered clouds";
public final static String BROKEN_CLOUDS = "broken clouds";
public final static String SHOWER_RAIN = "shower rain";
public final static String RAIN = "rain";
public final static String THUNDERSTORM = "thunderstorm";
public final static String SNOW = "snow";
public final static String MIST = "mist";
public final static String WIND = "wind";
public final static String ANY_WEATHER = "any weather";
public final static String UNAVAILABLE = "unavailable";
private static Double longitude;
private static Double latitude;
public static final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
public static String getCurrentWeather(Context ctx) {
final JSONParser jsonParser = new JSONParser();
String return_weather = "";
String URL = "http://api.openweathermap.org/data/2.5/weather";
try {
// Building Parameters ( you can pass as many parameters as you
// want)
List<NameValuePair> params = new ArrayList<NameValuePair>();
LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null){
return (Weather.UNAVAILABLE);
}
longitude = location.getLongitude();
latitude = location.getLatitude();
params.add(new BasicNameValuePair("lat", latitude.toString()));
params.add(new BasicNameValuePair("lon", longitude.toString()));
params.add(new BasicNameValuePair("APPID",
"sampleappkey"));
// Getting JSON Object
JSONObject json = jsonParser.makeHttpRequest(URL, "GET", params);
//Log.e("Banana", json.toString());
// get icon
JSONArray weather = json.getJSONArray("weather");
JSONObject weather1 = (JSONObject) weather.get(0);
String icon = weather1.getString("icon");
//Log.e("Icon", icon);
//Log.e("Icon2", icon.substring(0, 2));
int iconNum = Integer.parseInt(icon.substring(0, 2));
switch (iconNum) {
case 1:
return_weather = Weather.CLEAR_SKY;
break;
case 2:
return_weather = Weather.FEW_CLOUDS;
break;
case 3:
return_weather = Weather.SCATTERED_CLOUDS;
break;
case 4:
return_weather = Weather.BROKEN_CLOUDS;
break;
case 9:
return_weather = Weather.SHOWER_RAIN;
break;
case 10:
return_weather = Weather.RAIN;
break;
case 11:
return_weather = Weather.THUNDERSTORM;
break;
case 13:
return_weather = Weather.SNOW;
break;
case 50:
return_weather = Weather.MIST;
break;
default:
return_weather = Weather.ANY_WEATHER;
break;
}
// get wind speed
JSONObject wind = (JSONObject) json.getJSONObject("wind");
Double speed = wind.getDouble("speed");
//Log.e("Wind speed", speed.toString());
//add condition to say wind when it's above certain speed and it's clear or cloudy
if ((iconNum <= 4)&&(speed >= 30)){
return_weather = Weather.WIND;
}
return (return_weather);
} catch (IOException e) {
e.printStackTrace();
return (Weather.UNAVAILABLE);
} catch (JSONException e) {
e.printStackTrace();
return (Weather.UNAVAILABLE);
}
}
}