我使用Volley GET方法并且我成功得到了响应。现在我尝试反序列化响应然后我尝试在另一个活动中传递自定义类
RequestQueue queue = Volley.newRequestQueue(getActivity());
mShowDialog();
String url = "************";
UTF8VolleySupport sr = new UTF8VolleySupport(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response", response + "mm");
hideDialog(pDialog);
TrainsMainClass trainsMainClass;
Gson gson = new Gson();
try {
JSONObject jsonObject = new JSONObject(response);
String errorcode = jsonObject.getString("errorcode");
if (errorcode.equals("500")) {
Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}
if (jsonObject.has("data")) {
trainsMainClass = gson.fromJson(jsonObject.getString("data"), TrainsMainClass.class);
if (trainsMainClass != null) {
Intent intent = new Intent(getActivity(), TrainsActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("trainsMainClass", trainsMainClass);
intent.putExtras(bundle);
startActivity(intent);
getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideDialog(pDialog);
NetworkResponse errorRes = error.networkResponse;
String stringData = "";
if (errorRes != null && errorRes.data != null) {
try {
stringData = new String(errorRes.data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Log.e("Error", stringData);
}
});
sr.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(sr);
public class TrainsActivity extends AppCompatActivity {
private TrainsMainClass trainsMainClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trains);
trainsMainClass = new TrainsMainClass();
if (getIntent() != null) {
trainsMainClass = (TrainsMainClass) getIntent().getExtras().getSerializable("trainsMainClass");
if (trainsMainClass != null)
Log.e("trainsMainClass", trainsMainClass + "");
}
}
}
public class TrainsMainClass implements Serializable{
@Override
public String toString() {
return "TrainsMainClass{" +
"trainDeparture=" + trainDeparture +
", trainReturn=" + trainReturn +
'}';
}
@SerializedName("departure")
private Timetable trainDeparture;
@SerializedName("return")
private Timetable trainReturn;
}
这是我的来源,但是当我运行我的应用程序时,我有例外。 我怎么能解决我的问题?
`Parcelable` encountered `IOException` writing serializable object
答案 0 :(得分:0)
为什么不直接在intent中传递jsonResponse字符串,以后可以在您正在开始的新活动中解析它!
intent.putExtraString("jsonResp", jsonObject.getString("data"));
startActivity(intent);
&安培;在您的其他活动中,您可以使用GSON将响应解析为对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trains);
trainsMainClass = new TrainsMainClass();
if (getIntent() != null) {
String response = getIntent().getExtraString("jsonResp");
if(response !=null){
trainsMainClass = gson.fromJson(, TrainsMainClass.class);
Log.e("trainsMainClass", trainsMainClass + "");
}
}
答案 1 :(得分:0)
尽量避免在活动之间传递大量数据,可以使用Intent
传递的数据大小有一个上限。或者,我建议做的是:
Activity
中的数据,解析它并存储在DB或内存存储中。确保数据中有一个有意义的ID字段。int
的ID(可能是long
,String
或Intent
)传递给第二个Activity
。Activity
中,从存储中按ID获取数据。此解决方案可以很好地适应您在活动之间传递的任何数据量,此外,鉴于您将数据保存在内存中,您将无需支付序列化和反序列化数据的成本Intent
。