2017-09-05 42 views
0
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ButterKnife.bind(this); 
     mProgressBar.setVisibility(View.INVISIBLE); 

     String soccerURL = "http://api.football-data.org/v1/competitions/444/leagueTable"; 

     if (isNetworkAvailable()) { 
      OkHttpClient client = new OkHttpClient(); 
      Request request = new Request.Builder() 
        .url(soccerURL) 
        .build(); 

      Call call = client.newCall(request); 
      call.enqueue(new Callback() { 
       @Override 
       public void onFailure(Request request, IOException e) { 

       } 

       @Override 
       public void onResponse(Response response) throws IOException { 
        try { 
         String jsonData = response.body().string(); 
         Log.v(TAG, jsonData); 
         if (response.isSuccessful()) { 
          mTableSoccer = getCurrentDetails(jsonData); 
         } else { 
          alertUserAboutError(); 
         } 
        } 
        catch (IOException e) { 
         Log.e(TAG, "Exception caught: ", e); 
        } 
        catch (JSONException e){ 
         Log.e(TAG, "Exception caught: ", e); 
        } 

       } 
      }); 

     } 
     else { 
      Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show(); 
     } 

     Log.d(TAG, "Main UI code is running!"); 

    } 

    private TableSoccer getCurrentDetails(String jsonData) throws JSONException { 

      JSONObject table = new JSONObject(jsonData); 
      String leagueCaption = table.getString("leagueCaption"); 
      Log.i(TAG, "From Json: " + leagueCaption); 

      JSONObject standing = table.getJSONObject("standing"); 

      TableSoccer tableSoccer = new TableSoccer(); 
      tableSoccer.setmPosition(standing.getDouble("position")); 
      tableSoccer.setmPoints(standing.getDouble("points")); 
      tableSoccer.setmWins(standing.getDouble("wins")); 
      tableSoccer.setmLosses(standing.getDouble("losses")); 


      return tableSoccer; 

    } 

    private boolean isNetworkAvailable() { 
     ConnectivityManager manager = (ConnectivityManager) 
       getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = manager.getActiveNetworkInfo(); 
     boolean isAvailable = false; 
     if (networkInfo !=null && networkInfo.isConnected()){ 
      isAvailable = true; 
     } 
     return isAvailable; 
    } 

    private void alertUserAboutError() { 
     AlertDialogFragment dialog = new AlertDialogFragment(); 
     dialog.show(getFragmentManager(), "error_dialog"); 
    } 
} 

如何從這個JSON數組鏈接 http://api.football-data.org/v1/competitions/444/leagueTable, 獲取數據,我嘗試代碼顯示的位置,點,目標,勝,平,負,teamName,goalsAgainst。我在JsonObject和JsonData的公共void onResponse中被困在這裏。我也有表足球類與獲取和設置好的一切。感謝任何幫助。JsonArray和JsonData實施

回答

0

首先你應該看看at this它會讓你瞭解JSON格式以及如何使用它。

讀完後,您會明白您的standing元素是JsonArray而不是JsonObject。所以你應該首先獲得數組,並開始提取它內部的對象,並根據需求使用它。下面的代碼可能會幫助你找出答案。

JSONObject table = new JSONObject(jsonData); 
    String leagueCaption = table.getString("leagueCaption"); 
    Log.i(TAG, "From Json: " + leagueCaption); 
    JSONArray standingArray = table.getJsonArray("standing"); 

    //this will be used to hold all the extracted objects inside standingArray. 
    ArrayList<TableSoccer> tableSoccerElements = new ArrayList<>(); 

    for(int i=0; i< standingArray.length(); i++){ 

     JSONObject standingObject = standingArray.getJSONObject(i); 

     TableSoccer tableSoccer = new TableSoccer(); 
     tableSoccer.setmPosition(standingObject.getDouble("position")); 
     tableSoccer.setmPoints(standingObject.getDouble("points")); 
     tableSoccer.setmWins(standingObject.getDouble("wins")); 
     tableSoccer.setmLosses(standingObject.getDouble("losses")); 

     tableSoccerElements.add(tableSoccer); 

    } 

    //here your tableSoccerElements array will be filled with the respective response number 
    //elements which is ready to use. 
+0

did not understand the question .. –

+0

「All good Aalap its working。Thank You」。 –

+0

很高興工作。謝謝。 –