我知道有很多可用的解决方案,但它们都没有帮助我我使用Gson解析json数据数据,我想将这些数据解析为另一个活动,但我得到了空指针异常。
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 point:
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;
}
}
答案 0 :(得分:3)
ref.child("users").observe(DataEventType.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
print("SNAPSHOT: \(snapshot)")
for snap in snapshot {
if let userDict = snap.value as? Dictionary<String, AnyObject> {
if userDict["Status"] as? Bool == false {
let key = snap.key
print(key)
//Add this key to userID array
}
}
}
}
}
答案 1 :(得分:2)
制作MyProfile.class
工具Serializable
确保您的回复为json
格式,然后在使用GsonBuilder()
的排球的onResponse()中
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
收到此类对象
Intent intent = getIntent();
MyProfile myprofile = (MyProfile )intent .getParcelableExtra("myprofileObject");