@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,,我尝试代码以显示位置,得分,进球,胜利,平局,损失,团队名称,目标。我被困在Jonobject和Jsondata中,在公共空白中。另外,我还有桌面足球课,并在那里设置一切。感谢任何帮助。
首先,您应该查看一下,它将使您了解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.