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實施
did not understand the question .. –
「All good Aalap its working。Thank You」。 –
很高興工作。謝謝。 –