我的應用程序的一部分出現問題,有一種情況是我在json流上使用http請求來獲取一些數據,並且它工作得很好。但在另一種情況下,我必須使用用戶的位置來獲取數據。並在使用位置提供程序後,http請求無法正常工作。 這個問題只出現在兩個設備中的一個上,我必須測試我的應用程序。在第一個,Android 2.3.6的智能手機,我沒有問題。但是在第二臺設備上,問題出現在這裏:星系標籤與Android 4.0.4(我做了最近的更新,它是3.0.1之前,問題是相同的)。和我的應用程序編譯與SDK 10(Android 2.3.3)http請求在使用gps後無法正常工作
我試圖捕捉異常是拋出錯誤時出現,但異常的消息爲空,我不能看到是什麼問題。 的anibody尚未遇到這種錯誤?
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);
}
};
和我的JSON解析器在那裏我做的HTTP連接的代碼:
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();
}
...
這裏是從活動的源代碼,可運行doRechercheGeoloc從上點擊一個按鈕啓動
}
thx。
不要把標籤科目。我非常懷疑任何人都會通過所有這些代碼。那麼剝離所有不相關的部分呢? –
我刪除了一些無用的代碼部分 – seb