[英]How to pass custom class in an intent
我正在使用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
為什么不直接傳遞意圖的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 + "");
}
}
盡量避免在Activity之間傳遞大量數據,使用Intent
可以傳遞的數據大小有一個上限。 另外,我建議做的是:
Activity
的數據,進行解析並將其存儲在DB或內存中。 確保您的數據中有一個有意義的ID字段。 Intent
的ID(可能為int
, long
或String
)傳遞給第二個Activity
。 Activity
,通過ID從存儲中獲取數據。 該解決方案可以很好地擴展您在Activity之間傳遞的任意數量的數據,此外,由於您會將數據保存在內存中,因此無需支付從Intent
進行序列化和反序列化的費用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.