0

我的应用程序的一部分有问题,在一种情况下,我在 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();
}

...

}

谢谢。

4

1 回答 1

-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
于 2012-11-07T15:44:29.300 回答