How to make Cricket app in Android Studio! using API

 To create an Android app to get cricket news and score updates, you'll need to use an API that provides cricket-related data. For this example, I'll demonstrate how to use the CricAPI service, which offers cricket-related data, including news and live scores. Please note that CricAPI might require an API key, so make sure to sign up on their website to obtain one.

Here's a step-by-step guide to creating a simple Android app using Android Studio:

Step 1: Set up the project

Create a new Android project in Android Studio. Name your project and choose the desired settings.


Step 2: Design the layout

Open `activity_main.xml` in the `res/layout` directory and design the user interface. For this example, we'll use a simple layout with a button to fetch the latest news and scores and a text view to display the results.


```xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="16dp">


    <Button

        android:id="@+id/fetchButton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:text="Fetch Cricket News and Scores" />


    <TextView

        android:id="@+id/resultTextView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@id/fetchButton"

        android:layout_marginTop="16dp" />


</RelativeLayout>

```


Step 3: Implement the MainActivity

Open `MainActivity.java` in the `java` folder and add the necessary code to fetch cricket news and scores from the CricAPI service.


```java

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;


import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;


import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.Scanner;


public class MainActivity extends AppCompatActivity {


    private static final String API_KEY = "YOUR_CRIC_API_KEY";

    private static final String CRICKET_NEWS_URL = "https://cricapi.com/api/cricketNews";

    private static final String CRICKET_SCORES_URL = "https://cricapi.com/api/matches";


    private TextView resultTextView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        resultTextView = findViewById(R.id.resultTextView);

        Button fetchButton = findViewById(R.id.fetchButton);


        fetchButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                new FetchCricketDataTask().execute();

            }

        });

    }


    private class FetchCricketDataTask extends AsyncTask<Void, Void, String> {

        @Override

        protected String doInBackground(Void... voids) {

            String newsAndScores = "";


            try {

                String newsJson = fetchDataFromUrl(CRICKET_NEWS_URL);

                String scoresJson = fetchDataFromUrl(CRICKET_SCORES_URL);


                // Process the JSON data here to extract relevant information

                // For simplicity, I'll just concatenate the raw JSON strings

                newsAndScores = newsJson + scoresJson;

            } catch (IOException e) {

                e.printStackTrace();

            }


            return newsAndScores;

        }


        @Override

        protected void onPostExecute(String result) {

            resultTextView.setText(result)}

 }

    }


    private String fetchDataFromUrl(String urlString) throws IOException {

        URL url = new URL(urlString);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");

        connection.setDoInput(true);

        connection.connect();


        InputStream inputStream = connection.getInputStream();

        Scanner scanner = new Scanner(inputStream, "UTF-8");

        String jsonString = scanner.useDelimiter("\\A").next();


        scanner.close();

        inputStream.close();

        connection.disconnect();


        return jsonString;

    }

}

```

Make sure to replace `YOUR_CRIC_API_KEY` with your actual CricAPI key.


That's it! Run the app on an emulator or a physical device, and you should be able to fetch cricket news and scores by clicking the "Fetch Cricket News and Scores" button.


Note: This is a basic example that fetches raw JSON data from the CricAPI service. You can further enhance the app by parsing the JSON response and displaying specific information, such as news headlines and live scores, in a more user-friendly manner.

Post a Comment

Previous Post Next Post

Ads

Ad