不确定如何正确分割JSONObject来解决内存不足错误



我正在为Android创建一个GPS应用程序,该应用程序引导用户从当前位置到两个预定义位置之一。我从谷歌服务器下载的方向,然后暂时存储在一个JSONObject。如果方向变得太长或太复杂,则由于内存不足错误而导致应用程序崩溃。我想我需要使JSON对象可流或更改为JSON阅读器,但我已经搜索了一个答案,但我不明白如何实现。谁能提供一个使用下面代码的例子?谢谢你!

public DrivingDirection(JSONObject response) {
    try {
        JSONArray jsonArray = response.getJSONArray(TAG_ROUTES);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        summary = jsonObject.getString(TAG_SUMMARY);
        copyrights = jsonObject.getString(TAG_COPYRIGHTS);
        overviewPolyline =     decodePolyline(jsonObject.getJSONObject(TAG_OVERVIEW_POLYLINE).getString("points"));
        warnings = jsonObject.getJSONArray(TAG_WARNINGS);
        waypointOrder = jsonObject.getJSONArray(TAG_WAYPOINT_ORDER);
        JSONObject bounds = jsonObject.getJSONObject(TAG_BOUNDS);
        boundNorthEast = interpretLatLng(bounds.getJSONObject(TAG_BOUND_NORTH_EAST));
        boundSouthWest = interpretLatLng(bounds.getJSONObject(TAG_BOUND_SOUTH_WEST));
        JSONArray legs = jsonObject.getJSONArray(TAG_LEGS);
        directionLegs = new ArrayList<DirectionLeg>(legs.length());
        for (int i=0; i<legs.length(); i++){
            directionLegs.add(new DirectionLeg(legs.getJSONObject(i)));
        }
        directions = response.toString();
    } catch (JSONException e) {
       // Log.e(Debug.TAG, e.getMessage(), e);
    }
}

嗨,x-code,这是LogCat关于错误的输出,谢谢。

08-09 16:32:26.560: D/AndroidRuntime(2258): Shutting down VM
08-09 16:32:26.564: W/dalvikvm(2258): threadid=1: thread exiting with uncaught exception (group=0xa61f8908)
08-09 16:32:26.564: E/AndroidRuntime(2258): FATAL EXCEPTION: main
08-09 16:32:26.564: E/AndroidRuntime(2258): java.lang.IllegalStateException: Could not execute method of the activity
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.view.View$1.onClick(View.java:3599)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.view.View.performClick(View.java:4204)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.view.View$PerformClick.run(View.java:17355)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.os.Handler.handleCallback(Handler.java:725)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.os.Looper.loop(Looper.java:137)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invoke(Method.java:511)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at dalvik.system.NativeStart.main(Native Method)
08-09 16:32:26.564: E/AndroidRuntime(2258): Caused by: java.lang.reflect.InvocationTargetException
08-09 16:32:26.564: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invoke(Method.java:511)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at     android.view.View$1.onClick(View.java:3594)
08-09 16:32:26.564: E/AndroidRuntime(2258):     ... 11 more
08-09 16:32:26.564: E/AndroidRuntime(2258): Caused by: java.lang.OutOfMemoryError
08-09 16:32:26.564: E/AndroidRuntime(2258):     at oat.a(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at ova.j(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at oux.b(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at oux.a(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at oyf.a(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at grl.onTransact(SourceFile:137)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.os.Binder.transact(Binder.java:310)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addPolyline(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.L00081183.mygpstool.MainActivity.onClick_GetDirections(MainActivity.java:177)
08-09 16:32:26.564: E/AndroidRuntime(2258):     ... 14 more
08-09 16:32:26.816: I/dalvikvm-heap(2258): Clamp target GC heap from 257.458MB to 256.000MB
08-09 16:32:26.816: D/dalvikvm(2258): GC_CONCURRENT freed 446K, 1% free 261480K/262144K,     paused 1ms+10ms, total 252ms

这里是调用getDirections方法的地方

public void onClick_GetDirections(View v) {
    LocationManager lm;    
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location lastKnownLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    double longTemp = (double)(lastKnownLoc.getLongitude());
    double latTemp = (double)(lastKnownLoc.getLatitude());
    LatLng Start =  new LatLng(latTemp, longTemp);


            LatLng start = new LatLng(55.1082, -6.69342);
             LatLng end = new LatLng(54.509373, -8.180787);
             DirectionAPI directionAPI = new DirectionAPI(Start, LOCATION_LETTERKENNY);
             GoogleResponse googleResponse = directionAPI.execute();
             if (googleResponse.isOk()){
                 DrivingDirection drivingDirection =  new DrivingDirection(googleResponse.getJsonObject());
                 ArrayList<LatLng> polyline = drivingDirection.getTotalPolyline();
                 //PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
                 PolylineOptions rectline = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
                 for (int z = 0; z < polyline.size(); z++) {
                     LatLng point = polyline.get(z);
                     rectline.add(point);
                     GPSToolMap.addPolyline(rectline);
                 }
             //GP
}

}

Gson有一个流API -参见https://sites.google.com/site/gson/streaming

最新更新