我有JSON的问题

时间:2016-03-29 06:24:51

标签: java android arrays json

JSON存在问题。

HistoryFragment.java

private View rootView;
ListView list;
AppSharedPreference appSharedPreference;
private ProgressDialog progress;
String[] datetime;
String[] mobile;
String [] amount;
String[] operator;
String[] abpNo;
String[] optNo;
String[] rechargeStatus;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_history, container, false);

    appSharedPreference = AppSharedPreference.getInstance(getActivity());

    callHistoryApi();

    HistoryList adapter = new HistoryList(getActivity(),datetime,mobile,amount,operator,abpNo,optNo,rechargeStatus);
    list=(ListView)rootView.findViewById(R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

        }
    });
    return rootView;
}

@SuppressWarnings("unchecked")
private void callHistoryApi() {

    Map<String, String> param = null;
    try {
        param = new HashMap<String, String>();
        param.put("username", appSharedPreference.getUserName());
        param.put("trpass", appSharedPreference.getPassword());
        param.put("action", "RECH_HISTORY");
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    String jobjstr = param.toString();
    Log.e("LOGINREQUESTTTTTTT", jobjstr);
    if (jobjstr != null) {

        AllInOneAsyncTask asyncTask = new AllInOneAsyncTask(getActivity());
        asyncTask.setServiceResultListener(this);
        asyncTask.setServiceType(Constants.SERVICE_TYPE_GET_PROFILE);
        asyncTask.execute(param);
    }
}

@Override
public void onResult(String resultData, int requestType) {
    // TODO Auto-generated method stub
    if (requestType == Constants.SERVICE_TYPE_GET_PROFILE) {
        try {
            JSONObject jObject = new JSONObject(resultData);
            String Status = jObject.getString("Status");

            JSONArray history = jObject.getJSONArray("History");

            for (int i=0; i < history.length(); i++)
            {
                JSONObject oneObject = history.getJSONObject(i);
                String Datetime = oneObject.getString("Datetime");
                String Mobile = oneObject.getString("Mooble");
                String Amount = oneObject.getString("Amount");
                String Operator = oneObject.getString("Operator");
                String ABPNo = oneObject.getString("ABPNo");
                String OPTNO = oneObject.getString("OPTNO");
                String Recharge_Status = oneObject.getString("Recharge_Status");
                Log.e("HISTORYYYYYYYYYYYY",Datetime+" "+Mobile+" "+Amount+" "+Operator+" "+ABPNo+" "+
                        OPTNO+" "+Recharge_Status);
                datetime = new String[history.length()];
                mobile = new String[history.length()];
                amount = new String[history.length()];
                operator = new String[history.length()];
                abpNo = new String[history.length()];
                optNo = new String[history.length()];
                rechargeStatus = new String[history.length()];
                datetime[i] = Datetime;
                mobile[i] = Mobile;
                amount[i] = Amount;
                operator[i] = Operator;
                abpNo[i] = ABPNo;
                optNo[i] = OPTNO;
                rechargeStatus[i] = Recharge_Status;
            }
        }
        catch(Exception e)
        {
        }
    }
}

HistoryList.java

public HistoryList(Activity context,String[] datetime,String[] mobile,String[] amount,String[] operator,String[] abpNo,
                   String[] optNo,String[] rechargeStatus) {
    super(context, R.layout.list_history, abpNo);
    this.context = context;
    this.datetime=datetime;
    this.mobile=mobile;
    this.amount = amount;
    this.operator=operator;
    this.abpNo=abpNo;
    this.optNo=optNo;
    this.rechargeStatus=rechargeStatus;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.list_history, null, true);

    TextView datetoday = (TextView) rowView.findViewById(R.id.date);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.id);
    TextView oper = (TextView) rowView.findViewById(R.id.operator);
    TextView amoun = (TextView) rowView.findViewById(R.id.amount);
    TextView statu = (TextView) rowView.findViewById(R.id.status);

    datetoday.setText(datetime[position]);
    txtTitle.setText("id ="+abpNo[position]);
    oper.setText(" operator ="+operator[position]);
    amoun.setText(" amount ="+amount[position]);
    statu.setText(" status ="+rechargeStatus[position]);


    return rowView;
}

这有一个问题,当我打开这个片段时出现错误尝试从空数组中读取如何解决这个问题。 请帮忙。

4 个答案:

答案 0 :(得分:0)

在HistryFragment.java中,您已经为每次循环迭代创建了datatime,mobile,.. etc数组。

for (int i=0; i < history.length(); i++)
            {
                JSONObject oneObject = history.getJSONObject(i);
                String Datetime = oneObject.getString("Datetime");
                String Mobile = oneObject.getString("Mooble");
                String Amount = oneObject.getString("Amount");
                String Operator = oneObject.getString("Operator");
                String ABPNo = oneObject.getString("ABPNo");
                String OPTNO = oneObject.getString("OPTNO");
                String Recharge_Status = oneObject.getString("Recharge_Status");
                Log.e("HISTORYYYYYYYYYYYY",Datetime+" "+Mobile+" "+Amount+" "+Operator+" "+ABPNo+" "+
                        OPTNO+" "+Recharge_Status);
                datetime = new String[history.length()];
                mobile = new String[history.length()];
                amount = new String[history.length()];
                operator = new String[history.length()];
                abpNo = new String[history.length()];
                optNo = new String[history.length()];
                rechargeStatus = new String[history.length()];
                datetime[i] = Datetime;
                mobile[i] = Mobile;
                amount[i] = Amount;
                operator[i] = Operator;
                abpNo[i] = ABPNo;
                optNo[i] = OPTNO;
                rechargeStatus[i] = Recharge_Status;
            }

这将是问题的原因。在循环之前创建数组并添加项目。

答案 1 :(得分:0)

这样做。

    datetime = new String[history.length()];
    mobile = new String[history.length()];
    amount = new String[history.length()];
    operator = new String[history.length()];
    abpNo = new String[history.length()];
    optNo = new String[history.length()];
    rechargeStatus = new String[history.length()];

    for (int i=0; i < history.length(); i++)
    {
        JSONObject oneObject = history.getJSONObject(i);
        String Datetime = oneObject.getString("Datetime");
        String Mobile = oneObject.getString("Mooble");
        String Amount = oneObject.getString("Amount");
        String Operator = oneObject.getString("Operator");
        String ABPNo = oneObject.getString("ABPNo");
        String OPTNO = oneObject.getString("OPTNO");
        String Recharge_Status = oneObject.getString("Recharge_Status");
            Log.e("HISTORYYYYYYYYYYYY",Datetime+" "+Mobile+" "+Amount+" "+Operator+" "+ABPNo+" "+
                    OPTNO+" "+Recharge_Status);

            datetime[i] = Datetime;
            mobile[i] = Mobile;
            amount[i] = Amount;
            operator[i] = Operator;
            abpNo[i] = ABPNo;
            optNo[i] = OPTNO;
            rechargeStatus[i] = Recharge_Status;
        }

您正在for循环中创建新数组 - 这是错误的。

答案 2 :(得分:0)

这里的问题是字符串数组没有得到值。请执行以下操作。 在循环开始之前初始化String数组,

    datetime = new String[history.length()];
    mobile = new String[history.length()];
    amount = new String[history.length()];
    operator = new String[history.length()];
    abpNo = new String[history.length()];
    optNo = new String[history.length()];
    rechargeStatus = new String[history.length()];

然后才开始你的循环迭代。 否则它将在循环的每次迭代中重置数组。

for (int i=0; i < history.length(); i++)
            {
                JSONObject oneObject = history.getJSONObject(i);
                String Datetime = oneObject.getString("Datetime");
                String Mobile = oneObject.getString("Mooble");
                String Amount = oneObject.getString("Amount");
                String Operator = oneObject.getString("Operator");
                String ABPNo = oneObject.getString("ABPNo");
                String OPTNO = oneObject.getString("OPTNO");
                String Recharge_Status = oneObject.getString("Recharge_Status");
                Log.e("HISTORYYYYYYYYYYYY",Datetime+" "+Mobile+" "+Amount+" "+Operator+" "+ABPNo+" "+
                        OPTNO+" "+Recharge_Status);

                datetime[i] = Datetime;
                mobile[i] = Mobile;
                amount[i] = Amount;
                operator[i] = Operator;
                abpNo[i] = ABPNo;
                optNo[i] = OPTNO;
                rechargeStatus[i] = Recharge_Status;
            }

快乐的编码......

答案 3 :(得分:0)

试试这个

首先打印datetime获取值的日志。

并在TextView中设置值之前给出条件,如下所示。

if(datetime[position]!=null){ 

    datetoday.setText(datetime[position]);

} 

可能datatime[position]返回null