从JSON打印数据有问题

时间:2014-05-06 03:43:33

标签: android json

下面我附上了我的代码。我想要实现的是将一个String数组打印到TextView,但是我甚至在它到达目前之前就得到了一个N​​PE。为了描述我的代码,数组masteriesArray [i] [x]拥有单独的掌握页面,i和与该页面对应的杰作x。我认为我遇到的问题是x是一个可变大小。我已经评论过我的代码,或许可以帮助它有所帮助。

错误在于:

   masteriesArray[i][x] = singleMastery.getString("name"); // Error here
   I have confirmed that i = 0, x = 0, and singleMastery.getString("name") is valid and in the case before the error returns "Block."

我的代码:

public class MasteriesActivity extends BaseActivity {

    private Spinner spinner;
    ArrayAdapter<String> adapter;

    public String[][] masteriesArray;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.masteries_activity);
        getActionBar().setTitle("Masteries");
        GetMasteries getMasteries = new GetMasteries();
        getMasteries.execute();
        spinner = (Spinner) findViewById(R.id.mastery_selector);
    }

    class GetMasteries extends AsyncTask<String, String, JSONObject> {

        private String api_key="d96236d2-6ee3-4cfd-afa7-f41bdbc11128";
        String region = MainActivity.region.toLowerCase();
        String id = StatsActivity.sumID;
        String encodedKey = null;
        String encodedRegion = null;
        String encodedId = null;
        String url = null;
        String url2 = null;


        // JSON Node Names
        String TAG_NAME = "name";
        String TAG_CURRENT = "current";


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                // Encode URL variables
                encodedId = URLEncoder.encode(id, "UTF-8");
                encodedKey = URLEncoder.encode(api_key, "UTF-8");
                encodedRegion = URLEncoder.encode(region, "UTF-8");

                url = "http://prod.api.pvp.net/api/lol/" + region + "/v1.4/summoner/" + id + "/masteries?api_key=" + api_key;
                url2 = "https://prod.api.pvp.net/api/lol/static-data/" + region + "/v1.2/mastery?api_key=" + api_key;

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            JSONParser jParser = new JSONParser();

            // Get JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            JSONObject masteriesList = jParser.getJSONFromUrl(url2);

            // Get JSON containing Rune Info and cache it
            try {
                ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile2.srl"));
                out.writeObject(masteriesList.toString());
                out.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            try {
                // Get JSON Object
                JSONObject masteries = json.getJSONObject(encodedId);

                // Get JSON Array node
                JSONArray mastery = masteries.getJSONArray("pages");

                // Load Saved file
                ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile2.srl")));
                String storedMasteryInfo = (String) in.readObject();
                JSONObject jsonObject = new JSONObject(storedMasteryInfo);
                JSONObject dataObject = jsonObject.getJSONObject("data");
                Log.i("Mastery data object", dataObject + "");

                // Loop through pages, page names stored in string array
                String[] name = new String[mastery.length()];
                String curr;
                ArrayList<String> masteryPageNames = new ArrayList<String>();

                Log.d("Mastery Length", mastery.length() + ""); // Number of pages user has:  20 Max
                masteriesArray = new String[mastery.length()][];


                for(int i = 0; i < mastery.length(); i++) {
                    JSONObject c = mastery.getJSONObject(i);
                    name[i] = c.getString(TAG_NAME);
                    curr = c.getString(TAG_CURRENT);

                    JSONArray masteryInfo = c.getJSONArray("masteries");

                    Log.d("Mastery Info", masteryInfo + ""); // Each Individual Page i

                    for(int x = 0; x < masteryInfo.length(); x++) {
                        JSONObject s = masteryInfo.getJSONObject(x);
                        String masteryId = s.getString("id");
                        JSONObject singleMastery = dataObject.getJSONObject(masteryId);
                        try {
                            Log.d("X", x + ""); // 0 then error
                            masteriesArray[i][x] = singleMastery.getString("name"); // Error here
                            Log.d("MasteriesArray[i][x]", masteriesArray[i][x]);
                            Log.d("I", i + "");
                        }
                        catch (JSONException e) {
                            masteriesArray[i][x] = "";
                        }
                    }

                    if(curr.equals("true"))
                        name[i] = name[i] + " [Active]";
                    masteryPageNames.add(name[i]);

                }

                adapter = new ArrayAdapter(MasteriesActivity.this,
                        android.R.layout.simple_spinner_dropdown_item,
                        masteryPageNames);

                spinner.setAdapter(adapter);

                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
                        ((TextView) adapterView.getChildAt(0)).setTextColor(Color.parseColor("#C49246"));
                        Toast.makeText(adapterView.getContext(),
                                "Page Selected: " + adapterView.getItemAtPosition(pos).toString(),
                                Toast.LENGTH_SHORT).show();
                        TextView masteriesText = (TextView) findViewById(R.id.masteries);
                        masteriesText.setText("Masteries: " + "\n");

                        for(int j = 0; j<masteriesArray.length; j++) {
                            Log.d("Iteration: ", j + "");
                            masteriesText.append(masteriesArray[pos][j]);
                            masteriesText.append("\n");
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

            } catch (JSONException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (OptionalDataException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

JSON:

{"23591778": {
   "pages": [
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4144,
               "rank": 1
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4111,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787712,
         "name": "Blind",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4233,
               "rank": 3
            },
            {
               "id": 4242,
               "rank": 1
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4251,
               "rank": 1
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4262,
               "rank": 1
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4252,
               "rank": 3
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4241,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            }
         ],
         "id": 34787713,
         "name": "AD/Tank Jungle",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4242,
               "rank": 1
            },
            {
               "id": 4233,
               "rank": 3
            },
            {
               "id": 4243,
               "rank": 1
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4213,
               "rank": 2
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4252,
               "rank": 2
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4113,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4123,
               "rank": 3
            },
            {
               "id": 4262,
               "rank": 1
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4133,
               "rank": 1
            },
            {
               "id": 4234,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            }
         ],
         "id": 34787714,
         "name": "Mumu",
         "current": true
      },
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4121,
               "rank": 1
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4144,
               "rank": 1
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4111,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787715,
         "name": "vi/j4/wuk/xin/noc",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4353,
               "rank": 3
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4311,
               "rank": 1
            },
            {
               "id": 4362,
               "rank": 1
            },
            {
               "id": 4322,
               "rank": 3
            },
            {
               "id": 4334,
               "rank": 1
            },
            {
               "id": 4332,
               "rank": 1
            },
            {
               "id": 4352,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4314,
               "rank": 1
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4331,
               "rank": 3
            },
            {
               "id": 4324,
               "rank": 1
            },
            {
               "id": 4313,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4342,
               "rank": 1
            },
            {
               "id": 4341,
               "rank": 1
            }
         ],
         "id": 34787716,
         "name": "Support",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4113,
               "rank": 4
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4142,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            }
         ],
         "id": 34787717,
         "name": "AD Jungle",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4121,
               "rank": 1
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4142,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787718,
         "name": "ADC",
         "current": false
      },
   ],
   "summonerId": 23591778
}}

0 个答案:

没有答案