gpt4 book ai didi

android - 如何使用Gson传递json对象数据?

转载 作者:行者123 更新时间:2023-11-29 14:44:04 25 4
gpt4 key购买 nike

我知道有很多可用的解决方案,但没有一个能帮助我使用 Gson 解析 json 数据,我也想将此数据解析到另一个 Activity ,但我遇到了空指针异常。

 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);
}

在解析这些数据时,我无法获得关于我的下一个 Activity 的解析对象:

 MyProfile object = getIntent().getExtras().getParcelable("profile");
userFirstName=object.getMyProfileDTO().get(0).getFirstName();

并在 bean 类上记录 cat 点:

 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;
}

}

最佳答案

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);

关于android - 如何使用Gson传递json对象数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47530864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com