问题
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();
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;
}
}
回答1:
Make your MyProfile.class
implement Serializable
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);
then start activity with intent
Intent i = new Intent(this, NextActivity.class);
i.putExtra("myprofileObject", myprofile );
startActivity(i);
and receive the object in NextActivity.class
like this
Intent intent = getIntent();
MyProfile myprofile = (MyProfile )intent .getSerializableExtra("myprofileObject");
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");
回答2:
Gson gson = new Gson();
MyProfile obj = new MyProfile();
String jsonInString = gson.toJson(obj);
// pass string object to next activity and convert string object to class.
//next activity write this code.
MyProfile obj= gson.fromJson(jsonInString, MyProfile.class);
回答3:
Instead of passing MyProfile Object as Parcelable, try as below in the sender class:
String data = new Gson().toJson(mMyProfile);
Intent intent = new Intent(getBaseContext(), EditProfileActivity.class);
intent.putExtra("profile ", data);
startActivity(intent);
At Receiver class, get your profile info as below:
String data = getIntent().getStringExtra("profile");
MyProfile object = new Gson().fromJson(data,MyProfile.class)
This might work for your case.
来源:https://stackoverflow.com/questions/47530864/how-to-pass-json-object-data-using-gson