我的应用程序的一部分有问题,在一种情况下,我在 json 流上使用 http 请求来获取一些数据,它工作得非常好。但在另一种情况下,我必须使用用户的位置来获取数据。使用位置提供程序后,http 请求无法正常工作。此问题仅出现在我必须测试我的应用程序的两个设备之一上。第一个,Android 2.3.6 的智能手机,我没有问题。但那是在出现问题的第二台设备上:带有 android 4.0.4 的 Galaxy 选项卡(我最近对其进行了更新,之前是 3.0.1,问题是一样的)。我的应用程序是用 sdk 10 (android 2.3.3) 编译的
我尝试捕获出现错误时引发的异常,但异常消息为空,我看不出问题出在哪里。有人遇到过这种错误吗?
这是活动的源代码,可运行的 doRechercheGeoloc 是通过单击按钮启动的:
private Runnable doRechercheGeoloc = new Runnable(){
public void run() {
// TODO Auto-generated method stub
getProvider = true;
LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location newLocation) {
// TODO Auto-generated method stub
Log.i("*", "GPS : location changed");
location = newLocation;
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.i("*", "GPS : provider disabled");
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.i("*", "GPS : provider enable");
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
Log.i("*", "GPS : status changed");
}
};
lm = (LocationManager) getSystemService( AnnuaireActivity.LOCATION_SERVICE );
Log.i("*", "le GPS est activé");
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lm.removeUpdates(locationListener);
lm = null;
getProvider = true;
lm = null;
handler.postAtFrontOfQueue(doRecherchePourGeoloc);
}
};
以及我进行 http 连接的 json 解析器的代码:
public class JsonParser {
...
private String getJson(String url) throws Exception{
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet( url );
try{
//this is this instruction that provoque the error
HttpResponse response = client.execute( get );
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("*", JsonParser.class.toString() + " : Failed to download file");
throw new Exception(JsonParser.class.toString() + " : Failed to download file");
}
}catch( ClientProtocolException e ){
Log.e("*", "JsonParser.getJson() : ClientProtocolException : " + e.getMessage());
throw e;//
}catch( IOException ioe ){
Log.e("*", "JsonParser.getJson() : IOException : " + ioe.getMessage());
throw ioe;//
}catch( Exception e ){
Log.e("*", "JsonParser.getJson() : Exception : " + e.getMessage());//the erreur is throwing from here and the message is null
throw e;
}
return builder.toString();
}
...
}
谢谢。