2017-05-13 55 views
0

我無法從(http://www.football-data.org)下載足球數據。我試圖通過爲每個團隊創建一個新的Standing對象並將它們添加到ArrayList來下載英格蘭超級聯賽表格。足球數據API JSON解析錯誤

我已經設置了我的解析器類,適配器類和我的視圖。

我得到的錯誤是:

org.json.JSONException: org.json.JSONException: Value {"_links":{"self":{"href":"http:\/\/api.football-data.org\/v1\/soccerseasons\/426\/leagueTable\/?matchday=37"},"soccerseason":{"href":"http:\/\/api.football-data.org\/v1\/soccerseasons\/426"}},"leagueCaption":"Premier League 2016\/17","matchday":37,"standing":[{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/61"}},"position":1,"teamName":"Chelsea FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/5\/5c\/Chelsea_crest.svg","playedGames":36,"points":87,"goals":76,"goalsAgainst":29,"goalDifference":47,"wins":28,"draws":3,"losses":5,"home":{"goals":46,"goalsAgainst":13,"wins":15,"draws":0,"losses":2},"away":{"goals":30,"goalsAgainst":16,"wins":13,"draws":3,"losses":3}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/73"}},"position":2,"teamName":"Tottenham Hotspur FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/b\/b4\/Tottenham_Hotspur.svg","playedGames":35,"points":77,"goals":71,"goalsAgainst":23,"goalDifference":48,"wins":23,"draws":8,"losses":4,"home":{"goals":45,"goalsAgainst":8,"wins":16,"draws":2,"losses":0},"away":{"goals":26,"goalsAgainst":15,"wins":7,"draws":6,"losses":4}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/65"}},"position":3,"teamName":"Manchester City FC","crestURI":"https:\/\/upload.wikimedia.org\/wikipedia\/en\/e\/eb\/Manchester_City_FC_badge.svg","playedGames":36,"points":72,"goals":72,"goalsAgainst":38,"goalDifference":34,"wins":21,"draws":9,"losses":6,"home":{"goals":34,"goalsAgainst":16,"wins":10,"draws":7,"losses":1},"away":{"goals":38,"goalsAgainst":22,"wins":11,"draws":2,"losses":5}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/64"}},"position":4,"teamName":"Liverpool FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/0\/0a\/FC_Liverpool.svg","playedGames":36,"points":70,"goals":71,"goalsAgainst":42,"goalDifference":29,"wins":20,"draws":10,"losses":6,"home":{"goals":42,"goalsAgainst":18,"wins":11,"draws":5,"losses":2},"away":{"goals":29,"goalsAgainst":24,"wins":9,"draws":5,"losses":4}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/57"}},"position":5,"teamName":"Arsenal FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/en\/5\/53\/Arsenal_FC.svg","playedGames":35,"points":66,"goals":68,"goalsAgainst":42,"goalDifference":26,"wins":20,"draws":6,"losses":9,"home":{"goals":34,"goalsAgainst":15,"wins":12,"draws":3,"losses":2},"away":{"goals":34,"goalsAgainst":27,"wins":8,"draws":3,"losses":7}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/66"}},"position":6,"teamName":"Manchester United FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/d\/da\/Manchester_United_FC.svg","playedGames":35,"points":65,"goals":51,"goalsAgainst":27,"goalDifference":24,"wins":17,"draws":14,"losses":4,"home":{"goals":24,"goalsAgainst":12,"wins":7,"draws":10,"losses":1},"away":{"goals":27,"goalsAgainst":15,"wins":10,"draws":4,"losses":3}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/62"}},"position":7,"teamName":"Everton FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/f\/f9\/Everton_FC.svg","playedGames":37,"points":61,"goals":61,"goalsAgainst":41,"goalDifference":20,"wins":17,"draws":10,"losses":10,"home":{"goals":42,"goalsAgainst":16,"wins":13,"draws":4,"losses":2},"away":{"goals":19,"goalsAgainst":25,"wins":4,"draws":6,"losses":8}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/74"}},"position":8,"teamName":"West Bromwich Albion FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/8\/8b\/West_Bromwich_Albion.svg","playedGames":36,"points":45,"goals":41,"goalsAgainst":46,"goalDifference":-5,"wins":12,"draws":9,"losses":15,"home":{"goals":27,"goalsAgainst":22,"wins":9,"draws":2,"losses":8},"away":{"goals":14,"goalsAgainst":24,"wins":3,"draws":7,"losses":7}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/1044"}},"position":9,"teamName":"AFC Bournemouth","crestURI":"https:\/ 
05-13 15:39:24.798 6021-6540/com.example.oisin.premierleaguesocial W/System.err:  at org.json.JSON.typeMismatch(JSON.java:111) 
05-13 15:39:24.798 6021-6540/com.example.oisin.premierleaguesocial W/System.err:  at org.json.JSONArray.<init>(JSONArray.java:96) 
05-13 15:39:24.798 6021-6540/com.example.oisin.premierleaguesocial W/System.err:  at org.json.JSONArray.<init>(JSONArray.java:108) 
05-13 15:39:24.798 6021-6540/com.example.oisin.premierleaguesocial W/System.err:  at com.example.oisin.premierleaguesocial.Utilities.JSONParser.parse(JSONParser.java:75) 
05-13 15:39:24.799 6021-6540/com.example.oisin.premierleaguesocial W/System.err:  at com.example.oisin.premierleaguesocial.Utilities.JSONParser.doInBackground(JSONParser.java:56) 
05-13 15:39:24.799 6021-6540/com.example.oisin.premierleaguesocial W/System.err:  at com.example.oisin.premierleaguesocial.Utilities.JSONParser.doInBackground(JSONParser.java:27) 

我一直在試圖解決這個問題了幾個小時,但不能修復它。我想我已經正確設置了我的數據類型。爲了簡單起見,我決定首先嚐試獲取Integer數據類型。

任何幫助將非常感激。謝謝。

public class JSONParser extends AsyncTask<Void, Void, Boolean> { 


    private Context c; 
    private String jsonData; 
    private RecyclerView rv; 

    private ProgressDialog pd; 
    private ArrayList<Standing> mLeagueTable = new ArrayList<>(); 

    public JSONParser(Context c, String jsonData, RecyclerView rv) { 
     this.c = c; 
     this.jsonData = jsonData; 
     this.rv = rv; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     pd = new ProgressDialog(c); 
     pd.setTitle("Parse"); 
     pd.setMessage("Parsing..Please Wait"); 
     pd.show(); 
    } 

    @Override 
    protected Boolean doInBackground (Void...params){ 

     return parse(); 
    } 

    @Override 
    protected void onPostExecute (Boolean isParsed){ 
     super.onPostExecute(isParsed); 

     pd.dismiss(); //Dismiss progress dialog. 
     if (isParsed) { 
      //BIND 
      rv.setAdapter(new TableAdapter(c, mLeagueTable)); //Pass in instance of adapter. 

     } else { 
      Toast.makeText(c, "Unable to Parse check your Log Output", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private Boolean parse() { 
     try { 
      //JSONArray ja = new JSONArray(jsonData); 
      JSONArray ja = new JSONArray("standing"); 
      JSONObject jo; //declare json OBJECT 

      mLeagueTable.clear(); //Clears the ArrayList. 
      Standing table;  //Declare a table 

      for (int i = 0; i < ja.length(); i++) //iterating in JSONArray 
      { 
       jo = ja.getJSONObject(i); 


       int position = ja.getInt(Integer.parseInt("position")); 
       int points = ja.getInt(Integer.parseInt("points")); 
       int playedGames = ja.getInt(Integer.parseInt("playedGames")); 
       int goals = ja.getInt(Integer.parseInt("goals")); 
       int goalsAgainst = ja.getInt(Integer.parseInt("goalsAgainst")); 
       int goalDifference = ja.getInt(Integer.parseInt("goalDifference")); 



       table = new Standing();  //Create a new "User" object. 

       table.setPosition(position); 
       table.setPoints(points); 
       table.setPlayedGames(playedGames); 
       table.setGoals(goals); 
       table.setGoalsAgainst(goalsAgainst); 
       table.setGoalDifference(goalDifference); 


       mLeagueTable.add(table); //Add new Standing object to ArrayList mLeagueTable. 
      } 
      return true; 

     } catch (JSONException e) { 
      e.printStackTrace(); 
      return false; 
     } 
    } 
} 







public class JSONDownloader extends AsyncTask<Void, Void, String> { 

    Context c; 
    String jsonURL; 
    RecyclerView rv; 


    ProgressDialog pd; 


    public JSONDownloader(Context c, String jsonURL, RecyclerView rv) { 
     this.c = c; 
     this.jsonURL = jsonURL; 
     this.rv = rv; 
    } 




    @Override 
    protected void onPreExecute() { //CALLED just before data is downloaded. 
     super.onPreExecute(); 

     pd = new ProgressDialog(c); 
     pd.setTitle("Download JSON"); 
     pd.setMessage("Downloading.... Please Wait!"); 
     pd.show(); 
    } 

    @Override 
    protected String doInBackground(Void... voids) { 
     return download(); 
    } 

    @Override 
    protected void onPostExecute(String jsonData) { 
     super.onPostExecute(jsonData); 

     pd.dismiss(); 
     if (jsonData.startsWith("Error")) 
     { 
      String error = jsonData; 
      Toast.makeText(c, error, Toast.LENGTH_SHORT).show(); 
     } else { 

      //PARSER 
      new JSONParser(c, jsonData, rv).execute(); 

     } 
    } 

    private String download() { 
     Object connection = Connector.connect(jsonURL); 
     if (connection.toString().startsWith("Error")) { 
      return connection.toString(); 
     } 

     try { 
      HttpURLConnection con = (HttpURLConnection) connection; //Cast connection to HTTPConnection 
      if (con.getResponseCode() == con.HTTP_OK) { 
       //GET INPUT FROM STREAM 
       InputStream is = new BufferedInputStream(con.getInputStream()); 
       BufferedReader br = new BufferedReader(new InputStreamReader(is)); 

       String line; 
       StringBuffer jsonData = new StringBuffer(); 

       // READ 
       while ((line = br.readLine()) != null) { 
        jsonData.append(line + "\n"); 
       } 

       //CLOSE RESOURCES 
       br.close(); 
       is.close(); 

       //RETURN JSON 
       return jsonData.toString(); 
      } else { 
       return "Error " + con.getResponseMessage(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return "Error " + e.getMessage(); 
     } 
    } 
} 


public class MainActivity extends AppCompatActivity { 

     String jsonURL = "http://api.football-data.org/v1/soccerseasons/426/leagueTable"; 

     public static final String TAG = "MainActivity"; 
     //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 

     private RecyclerView rv; 
     /*private ArrayList<Standing> mLeagueTable = new ArrayList<>(); //Initalise m 
     private TableAdapter mTableAdapter = new TableAdapter(mLeagueTable, this); */ 

     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_table); 
      //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      //setSupportActionBar(toolbar); 

      rv = (RecyclerView) findViewById(R.id.rv); 
      if (rv != null) { 
       rv.setLayoutManager(new LinearLayoutManager(this)); 
      } 
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); //Initialze FAB 
      fab.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        new JSONDownloader(MainActivity.this, jsonURL, rv).execute(); 
       } 
      }); 
     } 
    } 



public class TableAdapter extends RecyclerView.Adapter<MyViewHolder>{ 

    private ArrayList<Standing> mLeagueTable; 
    private Context c; 


    public TableAdapter(Context c, ArrayList<Standing> mLeagueTable) { 
     this.c = c; 
     this.mLeagueTable = mLeagueTable; 

    } //Constructor 



    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.tableitem, parent, false); //Inlfate the League Table model view. 
     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { //BINDS THE data to the appropriote View 


      Standing s = mLeagueTable.get(position); 

      final int teamPosition = s.getPosition(); 
      final String teamName = s.getTeamName(); 
      final int playedGames = s.getPlayedGames(); 
      final int goals = s.getGoals(); 
      final int goalsAgainst = s.getGoalsAgainst(); 
      final int goalDifference = s.getGoalDifference(); 
      final int points = s.getPoints(); 




      //holder.teamNameTxt.setText(teamName); 
      holder.teamPositionTxt.setText(teamPosition); 
      holder.playedGamesTxt.setText(playedGames); 
      //holder.teamImage.setImageResource(s.getTeamImage()); 
      holder.goalsTxt.setText(goals); 
      holder.goalsAgainstTxt.setText(goalsAgainst); 
      holder.goalDifferenceTxt.setText(goalDifference); 
      holder.pointsTxt.setText(points); 
      // holder.wins.setText(s.getWins()); 
      //holder.draws.setText(s.getDraws()); 
      //holder.losses.setText(s.getLosses()); 


     holder.setItemClickListener(new ItemClickListener() { 
      @Override 
      public void onItemClick(int pos) { 
       openActivity(teamPosition, playedGames, goals, goalsAgainst, goalDifference, points); 
      } 
     }); 



    } 



    @Override 
    public int getItemCount() {return mLeagueTable.size();} 




    ////open Activity 
    private void openActivity(int...details) 
    { 
     Intent i = new Intent(c, TableActivity.class); 
     i.putExtra("POSITION_KEY", details[0]); 
     //i.putExtra("TEAMNAME_KEY", details[1]); 
     i.putExtra("POINTS_KEY", details[2]); 
     i.putExtra("PLAYEDGAMES_KEY", details[3]); 
     i.putExtra("GOALS_KEY", details[4]); 
     i.putExtra("GOALSAGAINST_KEY", details[5]); 
     i.putExtra("GOALDIFFERENCE_KEY", details[6]); 

     c.startActivity(i); 
    } 
} 

tableitem.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/teamPositionTxt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="@color/primary_text" /> 


    <TextView 
     android:id="@+id/teamNameTxt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     android:textColor="@color/primary_text" /> 

    <TextView 
     android:id="@+id/pointsTxt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="@color/primary_text" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/playedGamesTxt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="@color/primary_text" /> 

    <TextView 
     android:id="@+id/goalsTxt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="@color/primary_text" /> 

    <TextView 
     android:id="@+id/goalsAgainstTxt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="@color/primary_text" /> 

    <TextView 
     android:id="@+id/goalDifferenceTxt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="@color/primary_text" /> 

</LinearLayout> 

回答

1

1.從字符串jsonData創建一個JSONObject

2.通過使用jsonObj.getJSONArray("standing")JSONObject獲取JSONArraystanding)。

for循環,使用jo.getInt()代替ja.getInt()JsonObject得到int值。

這裏是工作代碼:

private Boolean parse() { 

    try { 
     JSONObject jsonObj = new JSONObject(jsonData); 

     JSONArray ja = jsonObj.getJSONArray("standing"); 

     mLeagueTable.clear(); //Clears the ArrayList. 

     for (int i = 0; i < ja.length(); i++) //iterating in JSONArray 
     { 
      JSONObject jo = ja.getJSONObject(i); 

      int position = jo.getInt(Integer.parseInt("position")); 
      int points = jo.getInt(Integer.parseInt("points")); 
      int playedGames = jo.getInt(Integer.parseInt("playedGames")); 
      int goals = jo.getInt(Integer.parseInt("goals")); 
      int goalsAgainst = jo.getInt(Integer.parseInt("goalsAgainst")); 
      int goalDifference = jo.getInt(Integer.parseInt("goalDifference")); 

      Standing table = new Standing();  //Create a new "User" object. 

      table.setPosition(position); 
      table.setPoints(points); 
      table.setPlayedGames(playedGames); 
      table.setGoals(goals); 
      table.setGoalsAgainst(goalsAgainst); 
      table.setGoalDifference(goalDifference); 


      mLeagueTable.add(table); //Add new Standing object to ArrayList mLeagueTable. 
     } 
     return true; 

    } catch (JSONException e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

希望這將工作〜

+0

是該代碼是正確的。謝謝! – Oisin834

0

變化

JSONArray ja = new JSONArray("standing"); 

JSONObject ja = new JSONObject(jsonData); 
JSONObject jo = ja.getJSONObject("soccerseason"); 

的信息是郭沫若,現在你可以提取以下的權利結構中的每個領域的JSONObject