Android 通过意图将JSONObject传递给另一个活动
我知道如何处理方法jsonObj.toString(),但我有理由不能这样做。我的JSONObject中有一个JSONArray,它包含太多的数据,所以如果我将其转换为字符串,然后再进行转换,我担心它会达到字符串类型的限制。 所以我想传递一个纯对象,而不是使用那个方法。我实现了Parcelable接口来实现这一点,但出现了错误“java.lang.RuntimeException:Parcel:无法封送值””Android 通过意图将JSONObject传递给另一个活动,android,json,string,Android,Json,String,我知道如何处理方法jsonObj.toString(),但我有理由不能这样做。我的JSONObject中有一个JSONArray,它包含太多的数据,所以如果我将其转换为字符串,然后再进行转换,我担心它会达到字符串类型的限制。 所以我想传递一个纯对象,而不是使用那个方法。我实现了Parcelable接口来实现这一点,但出现了错误“java.lang.RuntimeException:Parcel:无法封送值”” 公共类MJSONObject实现了可包裹{ 私有JSONObject JSONObje
公共类MJSONObject实现了可包裹{
私有JSONObject JSONObject;
公共对象{
}
公共MJSONObject(JSONObject JSONObject){
this.jsonObject=jsonObject;
}
公共无效集(JSONObject JSONObject){
this.jsonObject=jsonObject;
}
公共JSONObject get(){
返回jsonObject;
}
@凌驾
公共int描述内容(){
//TODO自动生成的方法存根
返回0;
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
公共MJSONObject createFromParcel(中的地块){
返回新的MJSONObject(in);
}
公共MJSONObject[]新数组(整数大小){
返回新的MJSONObject[大小];
}
};
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
dest.writeValue(jsonObject);
}
私人MJSONObject(地块位于){
.readValue(jsonObject.class.getClassLoader())中的jsonObject=(jsonObject);
}
试试这个,您还可以使用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(DrawerActivity.this, "store_object", 0);
ClassName className=complexPreferences.getObject("class_name", ClassName.class);
将对象作为意图传递,serilizable是最好的方法
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;
}
您可以通过将
JSONObject
转换为String
来轻松传递它,就像在某些情况下,我们通过将JSONObject
附加为String
来发送url一样。因此,尝试将其作为String
发送,如:
intent.putExtra("jsonObject", jsonObject.toString());
JSONObject jObj = new JSONObject(jsonString);
在另一边接受它
Intent intent = getIntent();
String jsonString = intent.getStringExtra("jsonObject");
现在,您的JSON
位于一个名为jsonString
的String
字符串中,假设它是一个响应,就像您从Web服务收到的响应一样,然后获取JSONObject
一样:
intent.putExtra("jsonObject", jsonObject.toString());
JSONObject jObj = new JSONObject(jsonString);
希望您能理解我的观点:)这有帮助吗?
intent.putExtra(“jsonObject”,“可序列化的”jsonObject)
我尝试过实现Serializable接口,也像这样传递它,但我遇到了类似的错误事实上,我实现了singleton模式作为解决问题的技巧,但我仍然想知道如何通过Intent将jsonobject传递给另一个活动。:DPlease show the error you gotU明白我的意思,但是java.lang.RuntimeException:Parcelable写入serializable对象(name=xxx.xxx.MJSONObject)时遇到IOException。哈哈,如果您使用serilizable从intent传递值,那么为什么要使用parcelable?上面的代码类似于我在项目中使用的东西。您需要在所有内部类中实现serilizable。很好,我实现了您的代码,然后出现了错误