gson.toJson(设置<customobject>)不起作用

时间:2015-09-21 07:32:59

标签: java android

我有一个customClass对象,它有Uri,int和&amp;其他customClass数据。我想使用GSON库保存共享参考中的Set。

final CustomClass obj1 = new CustomClass.Builder().mSourceUri(downloadUri)
                .mDestinationURI(destinationUri)
                .metaData(metaData1).build();
Set<CustomClass> set = new HashSet<CustomClass>();
set.add(obj1);
set.add(obj2);..etc    
Gson gson = new Gson();    
        Type type = new TypeToken<Set<CustomClass>>() {}.getType();    
        String json = gson.toJson(set,type);
        editor = sharedPreferences.edit();
        editor.putString("ShareKey", json);
        editor.commit();

但是gson.toJson()没有返回任何东西&amp;应用程序正在崩溃。 GC是连续调用的。我没有在我的应用程序中使用任何Generic类型。     当我尝试使用一些Student类数据的示例应用程序时,它工作正常。但是我的customClass对象总是失败请帮助解决这个问题。我复制了以下日志。

CustomObject类:

public class CustomObject implements Comparable<CustomObject> {
    private int mDownloadState;
    private int mDownloadId;
    private Uri mUri;    
    private Uri mDestinationURI;
    private RetryPolicy mRetryPolicy;
    private CustomObjectMetaData metaData;
    private boolean mCanceled = false;
    private CustomObjectQueue mRequestQueue;
    private DownloadStatusListener mDownloadListener;
    private HashMap<String, String> mCustomHeader;
    private enum Priority {
        LOW,
        NORMAL,
        HIGH,
        IMMEDIATE
    }

    private Priority mPriority = Priority.NORMAL;
    public static class Builder {
        private Uri mSourceUri;
        private Uri mDestinationURI;
        private CustomObjectMetaData metaData;

        public Builder Builder () {
            return this;
        }
        public Builder mSourceUri(Uri mUri) {
            this.mSourceUri = mUri;
            return this;
        }
        public Builder mDestinationURI(Uri mUri) {
            this.mDestinationURI = mUri;
            return this;
        }
        public Builder metaData(CustomObjectMetaData metaData) {
            this.metaData = metaData;
            return this;
        }

        public CustomObject build() {
            return new CustomObject(this);
        }
    }
    public CustomObject (Builder builder) {
        this.mUri = builder.mSourceUri;
        this.mDestinationURI = builder.mDestinationURI;
        this.metaData = builder.metaData;

        if (this.mUri == null) {
            throw new NullPointerException();
        }

        String scheme = this.mUri.getScheme();
        if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) {
            throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + mUri);
        }
        mCustomHeader = new HashMap<String, String>();
        mDownloadState = iDownloadManager.STATUS_PENDING;
    }
    public CustomObject () {
    }

    private Priority getPriority() {
        return mPriority;
    }


    private CustomObject setPriority(Priority priority) {
        mPriority = priority;
        return this;
    }


    public CustomObject addCustomHeader(String key, String value) {
        mCustomHeader.put(key, value);
        return this;
    }


    /**
     * Associates this request with the given queue. The request queue will be notified when this
     * request has finished.
     */
    void setCustomObjectQueue(CustomObjectQueue downloadQueue) {
        mRequestQueue = downloadQueue;
    }

    public RetryPolicy getRetryPolicy() {
        return mRetryPolicy == null ? new DefaultRetryPolicy() : mRetryPolicy;
    }

    private CustomObject setRetryPolicy(RetryPolicy mRetryPolicy) {
        this.mRetryPolicy = mRetryPolicy;
        return this;
    }

    /**
     * Sets the download Id of this request.  Used by {@link CustomObjectQueue}.
     */
    final void setDownloadId(int downloadId) {
        mDownloadId = downloadId;
    }

    final int getDownloadId() {
        return mDownloadId;
    }

    int getDownloadState() {
        return mDownloadState;
    }

    void setDownloadState(int mDownloadState) {
        //update meta with the state;

        this.mDownloadState = mDownloadState;
        getMetaData().setStatus(mDownloadState);

    }
    public CustomObjectMetaData getMetaData() {
        return metaData == null ? new CustomObjectMetaData() : this.metaData;
    }

    public CustomObject setMetaData(CustomObjectMetaData metaInfo) {
        this.metaData = metaInfo;
        this.metaData.setSourceUri(this.getSourceUri());
        this.metaData.setDestinationUri(this.getDestinationURI());
        return this;
    }

//    public CustomObjectMetaData getMetaData() {
//        return metaData;
//    }

    DownloadStatusListener getDownloadListener() {
        return mDownloadListener;
    }

    public CustomObject setDownloadListener(DownloadStatusListener downloadListener) {
        this.mDownloadListener = downloadListener;
        return this;
    }

    public Uri getSourceUri() {
        return mUri;
    }

    public CustomObject setSourceUri(Uri mUri) {
        this.mUri = mUri;
        return this;
    }

    public Uri getDestinationURI() {
        return mDestinationURI;
    }

    public CustomObject setDestinationURI(Uri destinationURI) {
        this.mDestinationURI = destinationURI;
        return this;
    }

    //Package-private methods.

    /**
     * Mark this request as canceled.  No callback will be delivered.
     */
    public void cancel() {
        mCanceled = true;
    }

    public void cancelDownLoad(int mDownloadId) {
        mRequestQueue.cancel(mDownloadId);
    }

    /**
     * Returns true if this request has been canceled.
     */
    public boolean isCanceled() {
        return mCanceled;
    }

    /**
     * Returns all custom headers set by user
     *
     * @return
     */
    HashMap<String, String> getCustomHeaders() {
        return mCustomHeader;
    }


    void finish() {
        mRequestQueue.finish(this);
    }

    @Override
    public int compareTo(CustomObject other) {
        Priority left = this.getPriority();
        Priority right = other.getPriority();

        // High-priority requests are "lesser" so they are sorted to the front.
        // Equal priorities are sorted by sequence number to provide FIFO ordering.
        return left == right ?
                this.mDownloadId - other.mDownloadId :
                right.ordinal() - left.ordinal();
    }
}


Log :     
    Suspending all threads took: 191.070ms
    09-21 12:43:04.827    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 10.456ms
    09-21 12:43:04.844    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 37651(1332KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 6MB/7MB, paused 12.266ms total 49.456ms
    09-21 12:43:05.029    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 69049(2MB) AllocSpace objects, 20(320KB) LOS objects, 37% free, 6MB/10MB, paused 9.733ms total 124.986ms
    09-21 12:43:05.308    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 87448(2MB) AllocSpace objects, 61(976KB) LOS objects, 21% free, 7MB/9MB, paused 11.171ms total 66.163ms
    09-21 12:43:05.445    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 45643(1402KB) AllocSpace objects, 32(512KB) LOS objects, 19% free, 7MB/9MB, paused 7.924ms total 50.964ms
    09-21 12:43:05.666    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 79067(2MB) AllocSpace objects, 54(864KB) LOS objects, 34% free, 7MB/11MB, paused 10.303ms total 140.975ms
    09-21 12:43:05.913    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 95791(2MB) AllocSpace objects, 60(960KB) LOS objects, 19% free, 8MB/10MB, paused 9.476ms total 59.863ms
    09-21 12:43:06.057    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 48515(1430KB) AllocSpace objects, 31(496KB) LOS objects, 17% free, 8MB/10MB, paused 10.450ms total 56.711ms
    09-21 12:43:06.291    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 88851(2MB) AllocSpace objects, 58(928KB) LOS objects, 32% free, 8MB/12MB, paused 10.302ms total 159.924ms
    09-21 12:43:06.545    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 96048(2MB) AllocSpace objects, 61(976KB) LOS objects, 18% free, 8MB/10MB, paused 10.662ms total 64.854ms
    09-21 12:43:06.697    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 47669(1412KB) AllocSpace objects, 31(496KB) LOS objects, 16% free, 9MB/10MB, paused 10.732ms total 62.647ms
    09-21 12:43:06.916    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 91.896ms
    09-21 12:43:06.924    4425-4445/com.mani.myApp.app W/art﹕ Suspending all threads took: 7.190ms
    09-21 12:43:06.940    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 59016(1610KB) AllocSpace objects, 32(512KB) LOS objects, 30% free, 9MB/13MB, paused 16.344ms total 167.829ms
    09-21 12:43:07.211    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 96708(2MB) AllocSpace objects, 58(928KB) LOS objects, 17% free, 9MB/11MB, paused 10.546ms total 74.644ms
    09-21 12:43:07.344    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 12.374ms
    09-21 12:43:07.352    4425-4445/com.mani.myApp.app W/art﹕ Suspending all threads took: 7.494ms
    09-21 12:43:07.367    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 52606(1436KB) AllocSpace objects, 28(448KB) LOS objects, 16% free, 9MB/11MB, paused 16.669ms total 65.283ms
    09-21 12:43:07.641    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 98017(2MB) AllocSpace objects, 60(960KB) LOS objects, 27% free, 10MB/14MB, paused 11.952ms total 187.184ms
    09-21 12:43:07.924    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 104842(2MB) AllocSpace objects, 57(912KB) LOS objects, 15% free, 10MB/12MB, paused 11.145ms total 71.245ms
    09-21 12:43:08.084    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 51645(1413KB) AllocSpace objects, 28(448KB) LOS objects, 14% free, 10MB/12MB, paused 11.965ms total 66.246ms
    09-21 12:43:08.362    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 32.383ms
    09-21 12:43:08.371    4425-4445/com.mani.myApp.app W/art﹕ Suspending all threads took: 9.035ms
    09-21 12:43:08.392    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 99862(2MB) AllocSpace objects, 57(952KB) LOS objects, 26% free, 11MB/15MB, paused 19.062ms total 219.195ms
    09-21 12:43:08.651    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 94185(2MB) AllocSpace objects, 51(1020KB) LOS objects, 14% free, 11MB/13MB, paused 10.410ms total 70.971ms
    09-21 12:43:08.805    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 47061(1378KB) AllocSpace objects, 25(500KB) LOS objects, 13% free, 11MB/13MB, paused 11.346ms total 63.690ms
    09-21 12:43:09.120    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 106179(3MB) AllocSpace objects, 60(1196KB) LOS objects, 24% free, 12MB/16MB, paused 13.157ms total 220.810ms
    09-21 12:43:09.363    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 29.424ms
    09-21 12:43:09.373    4425-4445/com.mani.myApp.app W/art﹕ Suspending all threads took: 9.951ms
    09-21 12:43:09.394    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 93791(2MB) AllocSpace objects, 46(920KB) LOS objects, 13% free, 12MB/14MB, paused 20.565ms total 80.242ms
    09-21 12:43:09.551    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 48697(1374KB) AllocSpace objects, 24(480KB) LOS objects, 12% free, 12MB/14MB, paused 13.081ms total 66.466ms
    09-21 12:43:09.847    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 13.442ms
    09-21 12:43:09.858    4425-4445/com.mani.myApp.app W/art﹕ Suspending all threads took: 10.652ms
    09-21 12:43:09.884    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 110673(3MB) AllocSpace objects, 59(1180KB) LOS objects, 23% free, 13MB/17MB, paused 23.900ms total 250.072ms
    09-21 12:43:10.164    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 100923(2MB) AllocSpace objects, 49(980KB) LOS objects, 12% free, 13MB/15MB, paused 13.358ms total 78.789ms
    09-21 12:43:10.333    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 50897(1411KB) AllocSpace objects, 24(480KB) LOS objects, 11% free, 14MB/15MB, paused 14.194ms total 73.668ms
    09-21 12:43:10.688    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 123400(3MB) AllocSpace objects, 64(1280KB) LOS objects, 21% free, 14MB/18MB, paused 12.810ms total 258.849ms
    09-21 12:43:10.990    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 105295(2MB) AllocSpace objects, 48(960KB) LOS objects, 11% free, 14MB/16MB, paused 13.258ms total 83.864ms
    09-21 12:43:11.165    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 54353(1492KB) AllocSpace objects, 25(500KB) LOS objects, 10% free, 15MB/16MB, paused 12.175ms total 75.996ms
    09-21 12:43:11.495    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 155.495ms
    09-21 12:43:11.506    4425-4445/com.mani.myApp.app W/art﹕ Suspending all threads took: 11.615ms
    09-21 12:43:11.529    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 63385(1880KB) AllocSpace objects, 36(720KB) LOS objects, 20% free, 15MB/19MB, paused 24.605ms total 275.336ms
    09-21 12:43:11.824    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 102216(2MB) AllocSpace objects, 47(940KB) LOS objects, 11% free, 15MB/17MB, paused 14.111ms total 81.338ms
    09-21 12:43:12.000    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 51526(1402KB) AllocSpace objects, 23(460KB) LOS objects, 10% free, 15MB/17MB, paused 13.084ms total 74.463ms
    09-21 12:43:12.374    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 33.642ms
    09-21 12:43:12.387    4425-4445/com.mani.myApp.app W/art﹕ Suspending all threads took: 13.049ms
    09-21 12:43:12.415    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 132547(3MB) AllocSpace objects, 61(1220KB) LOS objects, 19% free, 16MB/20MB, paused 25.429ms total 318.358ms
    09-21 12:43:12.722    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 107327(2MB) AllocSpace objects, 48(960KB) LOS objects, 10% free, 16MB/18MB, paused 15.514ms total 87.307ms
    09-21 12:43:12.878    4425-4440/com.mani.myApp.app W/art﹕ Suspending all threads took: 36.380ms
    09-21 12:43:12.891    4425-4445/com.mani.myApp.app W/art﹕ Suspending all threads took: 13.041ms
    09-21 12:43:12.912    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 39227(1141KB) AllocSpace objects, 20(420KB) LOS objects, 10% free, 17MB/18MB, paused 26.421ms total 84.216ms
    09-21 12:43:13.312    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 143804(3MB) AllocSpace objects, 62(1480KB) LOS objects, 18% free, 17MB/21MB, paused 15.782ms total 309.543ms
    09-21 12:43:13.622    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 103652(2MB) AllocSpace objects, 43(1032KB) LOS objects, 9% free, 18MB/20MB, paused 13.240ms total 89.262ms
    09-21 12:43:13.809    4425-4445/com.mani.myApp.app I/art﹕ Background sticky concurrent mark sweep GC freed 53375(1495KB) AllocSpace objects, 22(528KB) LOS objects, 8% free, 18MB/20MB, paused 15.340ms total 83.655ms
    09-21 12:43:14.241    4425-4445/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep 
    09-21 12:34:09.987  28979-28994/com.mani.myApp.app W/art﹕ Suspending all threads took: 3.798s
    09-21 12:34:10.175  28979-28999/com.mani.myApp.app W/art﹕ Suspending all threads took: 188.530ms
    09-21 12:34:10.418  28979-28999/com.mani.myApp.app I/art﹕ Clamp target GC heap from 259MB to 256MB
    09-21 12:34:10.418  28979-28999/com.mani.myApp.app I/art﹕ Background partial concurrent mark sweep GC freed 47(161KB) AllocSpace objects, 2(160KB) LOS objects, 0% free, 255MB/256MB, paused 237.084ms total 4.043s
    09-21 12:34:10.419  28979-28979/com.mani.myApp.app I/art﹕ WaitForGcToComplete

1 个答案:

答案 0 :(得分:0)

您的CustomObject中有许多复杂对象。如CustomObjectQueue,DownloadStatusListener等。我想这就是崩溃的原因。从您的CustomObject中删除复杂对象。希望这可以帮到你。