问题描述
我知道有很多可用的解决方案,但没有一个能帮助我我正在使用 Gson 解析 json 数据数据,我也想将此数据解析为另一个活动,但我遇到了空指针异常.
i know there is lots of solution available but none of them help me i am parsing json data data using Gson, and i want to parse this data to another activity as well but i am getting null pointer exception on that.
RequestQueue requestQueue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsOnbjRequest = new JsonObjectRequest(Request.Method.POST,
Constants.MyProfile,
userProfile,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
// Toast.makeText(mContext, response.toString(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
Gson mGson=new Gson();
mMyProfile=mGson.fromJson(response.toString(),MyProfile.class);
firstNameView.setText(mMyProfile.getMyProfileDTO().get(0).getFirstName());
lashNameView.setText(mMyProfile.getMyProfileDTO().get(0).getLastName());
adressNameView.setText(mMyProfile.getMyProfileDTO().get(0).getAddress());
countryNameView.setText(mMyProfile.getMyProfileDTO().get(0).getCountry());
zipCodeView.setText(mMyProfile.getMyProfileDTO().get(0).getZipCode());
emailIdView.setText(mMyProfile.getMyProfileDTO().get(0).getEmail());
phonNoview.setText(mMyProfile.getMyProfileDTO().get(0).getPhoneNumber());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(mContext, "ErrorMsg" + error.toString(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsOnbjRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.edit_profile, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
if (Connectivity.isConnected(mContext))
{
/*Intent userEditProfile = new Intent(mContext, EditProfileActivity.class);
userEditProfile.putExtra("userProfile",mMyProfile);
startActivity(userEditProfile);
overridePendingTransition(R.anim.right_to_left,
R.anim.left_to_right);*/
Intent intent = new Intent(getBaseContext(), EditProfileActivity.class);
intent.putExtra("profile ", mMyProfile);
startActivity(intent);
} else {
Connectivity.showNoConnection(mContext);
}
break;
}
return super.onOptionsItemSelected(item);
}
在解析这些数据时,我无法获得解析对象,因为我的下一个活动是这样的:
while parsing this data i am not able to get parsing object on that my next Activity is like:
MyProfile object = getIntent().getExtras().getParcelable("profile");
userFirstName=object.getMyProfileDTO().get(0).getFirstName();
并在 bean 类上记录 cat 点:
and log cat point on bean class:
public class MyProfile implements Parcelable
{
@SerializedName("Status")
@Expose
private String status;
@SerializedName("Message")
@Expose
private String message;
@SerializedName("myProfileDTO")
@Expose
private List<MyProfileDTO> myProfileDTO = null;
public final static Parcelable.Creator<MyProfile> CREATOR = new
Creator<MyProfile>() {
@SuppressWarnings({
"unchecked"
})
public MyProfile createFromParcel(Parcel in) {
return new MyProfile(in);
}
public MyProfile[] newArray(int size) {
return (new MyProfile[size]);
}
}
;
protected MyProfile(Parcel in) {
this.status = ((String) in.readValue((String.class.getClassLoader())));
this.message = ((String) in.readValue((String.class.getClassLoader())));
in.readList(this.myProfileDTO,
(MyProfileDTO.class.getClassLoader()));//here null pointer exception
}
public MyProfile() {
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<MyProfileDTO> getMyProfileDTO() {
return myProfileDTO;
}
public void setMyProfileDTO(List<MyProfileDTO> myProfileDTO) {
this.myProfileDTO = myProfileDTO;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(status);
dest.writeValue(message);
dest.writeList(myProfileDTO);
}
public int describeContents() {
return 0;
}
}
推荐答案
让你的 MyProfile.class
实现 Serializable
确保你的响应是 json
格式,然后在 volley 的 onResponse() 中使用 GsonBuilder()
像这样
make sure your response is in json
format and then in onResponse() of volley use GsonBuilder()
like this
MyProfile myprofile = new GsonBuilder().create().fromJson(response.toString(), MyProfile .class);
然后开始有意图的活动
Intent i = new Intent(this, NextActivity.class);
i.putExtra("myprofileObject", myprofile );
startActivity(i);
并像这样接收NextActivity.class
中的对象
Intent intent = getIntent();
MyProfile myprofile = (MyProfile )intent .getSerializableExtra("myprofileObject");
如果你将你的类实现为 Parcelable 然后像这样在 NextActivity.class
中接收对象
If you implement your class as Parcelable then receive the object in NextActivity.class
like this
Intent intent = getIntent();
MyProfile myprofile = (MyProfile )intent .getParcelableExtra("myprofileObject");
这篇关于如何使用 Gson 传递 json 对象数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!