通过Intent传递JSONObject到另一个活动

5

我知道如何使用jsonObj.toString()方法,但是由于我的JSONObject中包含一个数据量过大的JSONArray,因此如果将其转换为String类型再进行转换,我担心会达到String类型的限制。因此,我想传递一个纯粹的对象而不是使用该方法。我实现了Parcelable接口来实现这个目标,但是我遇到了错误"java.lang.RuntimeException: Parcel: unable to marshal value"。

public class MJSONObject implements Parcelable {
private JSONObject jsonObject;

public MJSONObject() {
}

public MJSONObject(JSONObject jsonObject) {
    this.jsonObject = jsonObject;
}

public void set(JSONObject jsonObject) {
    this.jsonObject = jsonObject;
}

public JSONObject get() {
    return jsonObject;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

public static final Parcelable.Creator<MJSONObject> CREATOR = new Parcelable.Creator<MJSONObject>() {
    public MJSONObject createFromParcel(Parcel in) {
        return new MJSONObject(in);
    }

    public MJSONObject[] newArray(int size) {
        return new MJSONObject[size];
    }
};

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(jsonObject);
}

private MJSONObject(Parcel in) {
    jsonObject = (JSONObject) in.readValue(JSONObject.class.getClassLoader());
}

这个有用吗?intent.putExtra("jsonObject", (Serializable) jsonObject); - Rakeeb Rajbhandari
我尝试了实现Serializable接口并像这样传递它,但是我得到了类似的错误。 - Slim_user71169
实际上,我实现单例模式来解决问题,但我仍然想知道如何通过Intent将JsonObject传递到另一个活动中。 :D - Slim_user71169
请展示您所遇到的错误。 - SweetWisher ツ
3个回答

36

您可以轻松地通过将JSONObject转换为String来传递它,就像在某些情况下我们将JSONObject附加为String发送到URL一样。 因此,请尝试将其作为String发送,例如:

intent.putExtra("jsonObject", jsonObject.toString());

并在另一端接收它

Intent intent = getIntent();

String jsonString = intent.getStringExtra("jsonObject");

现在您有一个命名为jsonStringString中的JSON,将其视为从Web服务接收响应时获得的内容,然后获取您的JSONObject,如下所示:

JSONObject jObj = new JSONObject(jsonString);

希望你能理解我的观点 :)

0

尝试这个,你也可以使用ComplexPrefereces类在一个活动中获取对象并传递到另一个活动

将ComplexPreferences.java类添加到您的项目中

import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;


/**
* This class helps to store class object in the shared preferences
*
*/

public class ComplexPreferences {

private static ComplexPreferences complexPreferences;
private Context context;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private static Gson GSON = new Gson();
Type typeOfObject = new TypeToken<Object>() {
}.getType();

private ComplexPreferences(Context context, String namePreferences, int mode) {
    this.context = context;
    if (namePreferences == null || namePreferences.equals("")) {
        namePreferences = "complex_preferences";
    }
    preferences = context.getSharedPreferences(namePreferences, mode);
    editor = preferences.edit();
}

public static ComplexPreferences getComplexPreferences(Context context,
                                                       String namePreferences, int mode) {

    if (complexPreferences == null) {
        complexPreferences = new ComplexPreferences(context,
                namePreferences, mode);
    }

    return complexPreferences;
}

public void putObject(String key, Object object) {
    if(object == null){
        throw new IllegalArgumentException("object is null");
    }

    if(key.equals("") || key == null){
        throw new IllegalArgumentException("key is empty or null");
    }

    editor.putString(key, GSON.toJson(object));
}

public void commit() {
    editor.commit();
}

public <T> T getObject(String key, Class<T> a) {

    String gson = preferences.getString(key, null);
    if (gson == null) {
        return null;
    } else {
        try{
            return GSON.fromJson(gson, a);
        } catch (Exception e) {
            throw new IllegalArgumentException("Object storaged with key " + key + " is instanceof other class");
        }
    }
}

}

像这样在共享首选项中存储对象:

    ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences(getActivity(), "store_object", 0);
    complexPreferences.putObject("class_name", className);
    complexPreferences.commit();

用于检索像这样的对象:

    ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences(DrawerActivity.this, "store_object", 0);
    ClassName className=complexPreferences.getObject("class_name", ClassName.class);

0

将对象作为意图传递,序列化是最好的方法。这里是从一个活动传递意图的代码

MJSONObject  mObject;
Intent intent = new Intent(FromActivity.this, ToActivity.class);
Bundle userObject = new Bundle();
userObject.putSerializable("Object", mObject);
userObject.putString("method_name", "ObjectIntent");
intent.putExtras(userObject);
startActivity(intent);

在另一个活动中,可以使用以下方式获取意图:
String method_name = null;
MJSONObject  mObject;
method_name = getIntent().getStringExtra("method_name");
if(method_name.equals("ObjectIntent"))
{
mObject= (MJSONObject) getObject.getSerializable("Object");
}

最后,您的自定义对象的POJO类应该是:

public class MJSONObject implements Serializable {

private static final long serialVersionUID = 1L;

private JSONObject jsonObject;

public MJSONObject() {
}

public MJSONObject(JSONObject jsonObject) {
    this.jsonObject = jsonObject;
}

public void set(JSONObject jsonObject) {
    this.jsonObject = jsonObject;
}

public JSONObject get() {
    return jsonObject;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

你明白我的意思,但是出现了java.lang.RuntimeException: Parcelable在写可序列化对象(名称=xxx.xxx.MJSONObject)时遇到IOException。哈哈 - Slim_user71169
如果您使用Serializable传递值,为什么要使用Parcelable呢? - Chandru
我在我的项目中也使用了类似上面的代码。 - Chandru
你需要在所有内部类中实现可序列化。 - Chandru
好的,我实现了你的代码,然后出现了错误。 - Slim_user71169
我不再使用Parcelable做任何事情了。 - Slim_user71169

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接