从JSONArray转换时,Parcelable类返回null

时间:2016-08-04 16:10:13

标签: java android arrays parsing parcelable

从Google表单下载的JSON数据

{
Sheet1: [
     {
        first_name: "Guy",
        last_name: "Man",
        first_n_last: "Guy Man",
        date: "2016-07-15T05:00:00.000Z",
        customer: "Company",
        jobtype: "Outage",
        ticket: "IN123456",
        status: "Incomplete"
      },
      {
        first_name: "duMMY",
        last_name: "dummy",
        first_n_last: "dummy",
        date: 1,
        customer: "dummy",
        jobtype: "dummy",
        ticket: "dumb",
        status: "dumb"
      }
     ]
  }

要存储JSON数据的Parcelable类'Jobs'

public class Jobs implements Parcelable {

public static final Creator<Jobs> CREATOR = new Creator<Jobs>() {
    @Override
    public Jobs createFromParcel(Parcel in) {
        return new Jobs(in);
    }

    @Override
    public Jobs[] newArray(int size) {
        return new Jobs[size];
    }
};

//Name
public String First_Name;
public String Last_Name;
public String First_and_Last_Name;
public int Date;
public String Customer;
public String Jobtype;
public String ticket;
public String JobStatus;
//

/**
 * No argument constructor for GSON (de-)serialization.
 */
@SuppressWarnings("unused")
public Jobs() {
}

/**
 * Private constructor used by Parcelable.Creator to deserialze a Parcelled User
 *
 * @param in - parcel to read from
 */
private Jobs(Parcel in) {
    this.First_Name = in.readString();
    this.Last_Name = in.readString();
    this.First_and_Last_Name = in.readString();
    this.Date = in.readInt();
    this.Customer = in.readString();
    this.Jobtype = in.readString();
    this.ticket = in.readString();
    this.JobStatus = in.readString();

}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(this.First_Name);
    dest.writeString(this.Last_Name);
    dest.writeString(this.First_and_Last_Name);
    dest.writeInt(this.Date);
    dest.writeString(this.Customer);
    dest.writeString(this.Jobtype);
    dest.writeString(this.ticket);
    dest.writeString(this.JobStatus);


}
}

主活动中的异步类,它调用下载和解析JSON的函数

private class AsyncUserDataLoader extends AsyncTask<String, Void, Jobs> {

    @Override
    protected Jobs doInBackground(String... params) {

        Jobs job = null;
        try {
           job = DataManager.getEmployeeData(params[0], params[1], params[2],
                    getActivity(), "true".equalsIgnoreCase(params[3]));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return job;
    }

    @Override
    protected void onPostExecute(Jobs result) {
        super.onPostExecute(result);

        // Update only if our context still exists.
        if (getActivity() != null) {
            ActiveFragment.this.mJobs = result;
            //updateViews();

        }

getEmployeeData函数

public static Jobs getEmployeeData(String urlString, String firstName, String lastName,
                                      Context context, boolean forceDownload) throws IOException {

        Jobs jobss;

        SharedPreferences prefs =
            context.getSharedPreferences(context.getString(R.string.app_global_storage_key), Context.MODE_PRIVATE);
        Gson gson = new Gson();

        String jsonString = downloadJsonDataEnt(urlString);

        jobss = findUserInArray(context, firstName, lastName, parseSpreadsheetJson(jsonString));


    return jobss;
}

在这里解析Json,然后传递给FindUserInArray函数

private static Jobs[] parseSpreadsheetJson(String jsonData) {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonObject map = parser.parse(jsonData).getAsJsonObject();
        JsonArray jobJsonArray = map.getAsJsonArray("Sheet1");

        return gson.fromJson(jobJsonArray, Jobs[].class);
    }

FindUserInArray方法,它使用可分配的作业数组

 private static Jobs findUserInArray(Context context, String firstName, String lastName,Jobs[] jobs) {

    for (int i = 0; i <= jobs.length; ++i) {
        Jobs job = jobs[i];

        if (job.Last_Name.equalsIgnoreCase(lastName) &&
                (job.First_Name.equalsIgnoreCase(firstName))) {
            SharedPreferences.Editor editor =
                    context.getSharedPreferences(context.getString(R.string.app_global_storage_key),
                            Context.MODE_PRIVATE)
                            .edit();
            editor.putInt(ActiveFragment.ROW_KEY, i);
            editor.apply();

            return job;
        }
    }
    return null;
}

显示Jobs类内容的错误日志为空

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
                                                                       at bricco.com.telicon.Model.DataManager.findUserInArray(DataManager.java:130)
                                                                       at bricco.com.telicon.Model.DataManager.getEmployeeData(DataManager.java:83)
                                                                       at bricco.com.telicon.ActiveFragment$AsyncUserDataLoader.doInBackground(ActiveFragment.java:279)
                                                                       at bricco.com.telicon.ActiveFragment$AsyncUserDataLoader.doInBackground(ActiveFragment.java:272)

0 个答案:

没有答案