我的应用程序的一部分有问题,在一种情况下我在json流上使用http请求来获取一些数据,它工作得非常好。但在另一种情况下,我必须使用用户的位置来获取数据。使用位置提供程序后,http请求无法正常工作。 这个问题只出现在我必须测试我的应用程序的两个设备之一。在第一个,Android 2.3.6的智能手机,我没有问题。但这是在seconde设备上,问题出现了:一个带有android 4.0.4的星系标签(我最近做了一次更新,之前是3.0.1,问题是一样的)。我的应用程序是用sdk 10(android 2.3.3)
编译的我尝试捕获错误出现时抛出的异常,但异常的消息为null,我不知道是什么问题。 anibody还遇到过这种错误吗?
这是来自活动的源代码,可通过点击按钮启动runnable 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);
}
};
和我的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();
}
...
}
THX。
答案 0 :(得分:-1)
您使用的是什么http包/ jar?弄清楚如何打开WIRE和HEADERS的日志记录。
我使用的软件包,我必须重新编译/使用一个单独的http jar来获取运行时的日志记录。
启用完整日志记录后,您应该有足够的数据来查找问题
请参阅adb logcat中的示例WIRE和HEADERS:
D/class ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient(18636): Attempt 1 to execute request
D/class ch.boye.httpclientandroidlib.impl.conn.DefaultClientConnection(18636): Sending request: POST /1/files/audio HTTP/1.1
D/ch.boye.httpclientandroidlib.wire(18636): >> "POST /1/files/audio HTTP/1.1[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "X-Parse-Application-Id: 3KxPBTPSTe8f0iexGanSagCztLp6wSPzJkyMLAbR[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "X-Parse-REST-API-Key: kVl5Z0CXmBSCoQRmE8XSLIDFuLGHMCIkLXXjkuI9[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Content-Type: audio/*[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Content-Length: 12074[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Host: api.parse.com[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Connection: Keep-Alive[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.headers(18636): >> POST /1/files/audio HTTP/1.1
D/ch.boye.httpclientandroidlib.headers(18636): >> X-Parse-Application-Id: 3KxPBTPSTe8f0iexGanSagCztLp6wSPzJkyMLAbR
D/ch.boye.httpclientandroidlib.headers(18636): >> X-Parse-REST-API-Key: kVl5Z0CXmBSCoQRmE8XSLIDFuLGHMCIkLXXjkuI9
D/ch.boye.httpclientandroidlib.headers(18636): >> Content-Type: audio/*
D/ch.boye.httpclientandroidlib.headers(18636): >> Content-Length: 12074
D/ch.boye.httpclientandroidlib.headers(18636): >> Host: api.parse.com
D/ch.boye.httpclientandroidlib.headers(18636): >> Connection: Keep-Alive
D/ch.boye.httpclientandroidlib.wire(18636): >> "--"
D/ch.boye.httpclientandroidlib.wire(18636): >> "cVxX6b-jxQnxFCczaKHLNZ_Hq8HI9AEW219GW3w"
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Content-Disposition"
D/ch.boye.httpclientandroidlib.wire(18636): >> ": "
D/ch.boye.httpclientandroidlib.wire(18636): >> "form-data; name="bin"; filename="myfile.3gp""
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "Content-Type"
D/ch.boye.httpclientandroidlib.wire(18636): >> ": "
D/ch.boye.httpclientandroidlib.wire(18636): >> "application/octet-stream"
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/ch.boye.httpclientandroidlib.wire(18636): >> "[\r][\n]"
D/dalvikvm(18636): GC_CONCURRENT freed 387K, 5% free 11211K/11719K, paused 12ms+3ms, total 38ms
D/dalvikvm(18636): WAIT_FOR_CONCURRENT_GC blocked 12ms
D/ch.boye.httpclientandroidlib.wire(18636): >> "[0x0][0x0][0x0][0x18]ftyp3gp4[0x0][0x0][0x0][0x0]isom3gp4[0x0][0x0][0x2][0xe7]moov[0