Live Cricket Score Example
This example will show you how can you build a cricket live score application in android using JSON Parsing.
Quick steps to perform:-
1) Create two additional java files. The first file will use to fetch data in the background and the second is used to show the data.
2) Create one Layout file to show data.
3) Search the Live Score API for your project.
First open the main.xml file and add listview to show the current live match.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:textColor="@android:color/white"
android:layout_gravity="center"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_marginTop="10dp"
android:background="@android:color/holo_orange_dark"
android:layout_below="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="@android:color/black"
android:dividerHeight="20dp" />
</RelativeLayout>
Open the Main.java file to set the adapter to the list-view:-
Main.java
//used to store name
ArrayList<String> name=new ArrayList<>();
//used to store id
ArrayList<String> id=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar();
//Url fetch the data
String url="http://cricscore-api.appspot.com/csa";
TextView tv= (TextView) findViewById(R.id.textView);
ListView listmatches= (ListView) findViewById(R.id.listView);
// Async task to fetch data in background
Myclass my=new Myclass();
my.execute(url);
String jsonstring=null;
try {
jsonstring = my.get();
tv.setText("Live Matches");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
try {
jsonstring=jsonstring.substring(jsonstring.indexOf("["),jsonstring.length());
JSONArray array=new JSONArray(jsonstring);
for(int i=0;i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
String matchname="\t"+jsonObject.optString("t1").toString() +" VS " +
jsonObject.optString("t2").toString();
name.add(matchname);
id.add(jsonObject.optString("id").toString());
}
listmatches.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,name));
listmatches.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id2) {
Intent istart=new Intent(MainActivity.this,scorecard.class);
istart.putExtra("id", id.get(position));
istart.putExtra("name", name.get(position));
startActivity(istart);
//Toast.makeText(MainActivity.this, id.get(position), Toast.LENGTH_LONG).show(); }
});
} catch (JSONException e) {
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
Create the second, Myclass to fetch data from online.
Myclass.java
public class Myclass extends AsyncTask<String ,Void,String> {
String finalString;
@Override
protected String doInBackground(String... params) {
try {
URL scoreurl = new URL(params[0]);
URLConnection connection=scoreurl.openConnection();
BufferedReader bf=new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String tmp=bf.readLine();
while (tmp != null) {
finalString=finalString+tmp;
tmp=bf.readLine();
}
}
catch (Exception e)
{
e.getMessage();
}
return finalString;
}
Create XML file to show data score.helper.xml:-
Score_helper.xml
<LinearLayout
android:background="@android:color/holo_orange_dark">
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:textSize="20dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView8"
android:textSize="15dp"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 1"
android:id="@+id/Player1"
android:textSize="20dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView3"
android:layout_marginLeft="30dp"
android:textSize="20dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 2 "
android:id="@+id/Player2"
android:textSize="20dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView5"
android:textSize="20dp"
android:layout_marginLeft="30dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView4"
android:textSize="20dp"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_centerHorizontal="true"
android:src="@drawable/cricketworldcup"
android:background="@android:color/holo_orange_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"
android:layout_alignParentBottom="true" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"/>
</RelativeLayout>
</LinearLayout>
Create a java file to open the selected match and show results.
Scorecard.java
public class scorecard extends AppCompatActivity{
TextView title, player1, bolwer, player2, play2, scorecard, play1;
String name, id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scorecard_helper);
Bundle b = getIntent().getExtras();
id = b.getString("id");
name = b.getString("name");
title = (TextView) findViewById(R.id.textView2);
player1 = (TextView) findViewById(R.id.textView3);
bolwer = (TextView) findViewById(R.id.textView4);
player2 = (TextView) findViewById(R.id.textView5);
play2 = (TextView) findViewById(R.id.Player2);
scorecard = (TextView) findViewById(R.id.textView8);
play1 = (TextView) findViewById(R.id.Player1);
doing();
final android.os.Handler handle = new android.os.Handler();
new CountDownTimer(10000, 5000) {
@Override
public void onTick(long millisUntilFinished) {
doing();
}
@Override
public void onFinish() {
start();
}
}.start();
}
public void doing() {
title.setText(name);
String url = "http://cricscore-api.appspot.com/csa?id=" + id;
Myclass my = new Myclass();
my.execute(url);
String jsonstring = null;
try {
jsonstring = my.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
try {
jsonstring = jsonstring.substring(jsonstring.indexOf("["), jsonstring.length());
// JSONObject object=new JSONObject(jsonstring);
//JSONArray array=object.optJSONArray("");
JSONArray array = new JSONArray(jsonstring);
// JSONArray array=new JSONArray(jsonstring);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String matchname = "---> \t" + jsonObject.optString("t1").toString() + VS "
+ jsonObject.optString("t1").toString();
scorecard.setText(jsonObject.optString("de").toString());
String[] mystring = jsonObject.optString("de").toString().split(",");
player1.setText(mystring[1]);
player2.setText(mystring[2]);
if (mystring.length < 4) {
bolwer.setVisibility(TextView.INVISIBLE);
play2.setVisibility(TextView.INVISIBLE);
play1.setVisibility(TextView.INVISIBLE);
} else {
bolwer.setText(mystring[3]);
}
}
} catch (JSONException e) {
Toast.makeText(scorecard.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Quick steps to perform:-
1) Create two additional java files. The first file will use to fetch data in the background and the second is used to show the data.
2) Create one Layout file to show data.
3) Search the Live Score API for your project.
First open the main.xml file and add listview to show the current live match.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:textColor="@android:color/white"
android:layout_gravity="center"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_marginTop="10dp"
android:background="@android:color/holo_orange_dark"
android:layout_below="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="@android:color/black"
android:dividerHeight="20dp" />
</RelativeLayout>
Open the Main.java file to set the adapter to the list-view:-
Main.java
//used to store name
ArrayList<String> name=new ArrayList<>();
//used to store id
ArrayList<String> id=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar();
//Url fetch the data
String url="http://cricscore-api.appspot.com/csa";
TextView tv= (TextView) findViewById(R.id.textView);
ListView listmatches= (ListView) findViewById(R.id.listView);
// Async task to fetch data in background
Myclass my=new Myclass();
my.execute(url);
String jsonstring=null;
try {
jsonstring = my.get();
tv.setText("Live Matches");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
try {
jsonstring=jsonstring.substring(jsonstring.indexOf("["),jsonstring.length());
JSONArray array=new JSONArray(jsonstring);
for(int i=0;i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
String matchname="\t"+jsonObject.optString("t1").toString() +" VS " +
jsonObject.optString("t2").toString();
name.add(matchname);
id.add(jsonObject.optString("id").toString());
}
listmatches.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,name));
listmatches.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id2) {
Intent istart=new Intent(MainActivity.this,scorecard.class);
istart.putExtra("id", id.get(position));
istart.putExtra("name", name.get(position));
startActivity(istart);
//Toast.makeText(MainActivity.this, id.get(position), Toast.LENGTH_LONG).show(); }
});
} catch (JSONException e) {
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
Create the second, Myclass to fetch data from online.
Myclass.java
public class Myclass extends AsyncTask<String ,Void,String> {
String finalString;
@Override
protected String doInBackground(String... params) {
try {
URL scoreurl = new URL(params[0]);
URLConnection connection=scoreurl.openConnection();
BufferedReader bf=new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String tmp=bf.readLine();
while (tmp != null) {
finalString=finalString+tmp;
tmp=bf.readLine();
}
}
catch (Exception e)
{
e.getMessage();
}
return finalString;
}
Create XML file to show data score.helper.xml:-
Score_helper.xml
<LinearLayout
android:background="@android:color/holo_orange_dark">
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:textSize="20dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView8"
android:textSize="15dp"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 1"
android:id="@+id/Player1"
android:textSize="20dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView3"
android:layout_marginLeft="30dp"
android:textSize="20dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player 2 "
android:id="@+id/Player2"
android:textSize="20dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView5"
android:textSize="20dp"
android:layout_marginLeft="30dp"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView4"
android:textSize="20dp"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_centerHorizontal="true"
android:src="@drawable/cricketworldcup"
android:background="@android:color/holo_orange_dark" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"
android:layout_alignParentBottom="true" />
<View
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@android:color/holo_orange_light"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"/>
</RelativeLayout>
</LinearLayout>
Create a java file to open the selected match and show results.
Scorecard.java
public class scorecard extends AppCompatActivity{
TextView title, player1, bolwer, player2, play2, scorecard, play1;
String name, id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scorecard_helper);
Bundle b = getIntent().getExtras();
id = b.getString("id");
name = b.getString("name");
title = (TextView) findViewById(R.id.textView2);
player1 = (TextView) findViewById(R.id.textView3);
bolwer = (TextView) findViewById(R.id.textView4);
player2 = (TextView) findViewById(R.id.textView5);
play2 = (TextView) findViewById(R.id.Player2);
scorecard = (TextView) findViewById(R.id.textView8);
play1 = (TextView) findViewById(R.id.Player1);
doing();
final android.os.Handler handle = new android.os.Handler();
new CountDownTimer(10000, 5000) {
@Override
public void onTick(long millisUntilFinished) {
doing();
}
@Override
public void onFinish() {
start();
}
}.start();
}
public void doing() {
title.setText(name);
String url = "http://cricscore-api.appspot.com/csa?id=" + id;
Myclass my = new Myclass();
my.execute(url);
String jsonstring = null;
try {
jsonstring = my.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
try {
jsonstring = jsonstring.substring(jsonstring.indexOf("["), jsonstring.length());
// JSONObject object=new JSONObject(jsonstring);
//JSONArray array=object.optJSONArray("");
JSONArray array = new JSONArray(jsonstring);
// JSONArray array=new JSONArray(jsonstring);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String matchname = "---> \t" + jsonObject.optString("t1").toString() + VS "
+ jsonObject.optString("t1").toString();
scorecard.setText(jsonObject.optString("de").toString());
String[] mystring = jsonObject.optString("de").toString().split(",");
player1.setText(mystring[1]);
player2.setText(mystring[2]);
if (mystring.length < 4) {
bolwer.setVisibility(TextView.INVISIBLE);
play2.setVisibility(TextView.INVISIBLE);
play1.setVisibility(TextView.INVISIBLE);
} else {
bolwer.setText(mystring[3]);
}
}
} catch (JSONException e) {
Toast.makeText(scorecard.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Output :-
Main Screen Second Screen(Scorecard)
live cricket Score
ReplyDeleteKcricketscore keeps you updated fast live cricket Score, Live ipl score in 2019, and Live world cup score in 2019. You can check any cricket match live Score like IPL and world cup score in 2019. visit our website.
ICC Cricket World Cup 2019 All Team Squad
ReplyDeleteSuccessful India Cricket Team Captains
Cricket World Cup History
2019 Cricket World Cup Teams
Cricket World Cup 2019 Schedule
Cricket World Cup 2019 India Time Table
ReplyDeleteICC Cricket World Cup 2019 Tickets Sale
i am getting error
ReplyDeleteCaused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.indexOf(java.lang.String)' on a null object reference
at com.appriverr.iccworldcupcricket.LiveScoreActivity.onCreate(LiveScoreActivity.java:48)
please help me to solve it..
thanks
live cricket Scores
ReplyDeleteGet Today's latest cricket news and headlines. kcricketscore providing Daily update for any cricket News Like this World cup Live scores, live cricket Score, Upcoming cricket Matches 2019.
It's a great blog and very informative. Thanks for Sharing.
ReplyDeleteWe provide the best services for live score API on game betting. Contact us at BetsAPI and enjoy live betting API on games with professionals!