How to fetch data from a JSON file to Android using the URL with Android Studio

in #android8 years ago (edited)

What is JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999.

What Will I Learn?

  • How to Fetch JSON data to your Android App

Requirements

  • Working Android Studio
  • Basic Knowledge of Android Studio
  • Save the data in JSON Format

Procedure

  • Start a new Android Project and name it of your own choice.
    new proj.png

  • Now open the XML file of your MainActivity and add a Button and TextView in your Design. You can also copy this code for Testing

  • If your Design Preview is Blank then change the Theme to "Holo Dark"
    theme1.png

  • Then Design your Layout and add a Button and a TextView where we fetch our JSON data by clicking that Button and give them id. Follows
    mobile.png


    xml.png

  • Now Register these Elements in the Main Activity, initialize them and implements the OnClickListener on the fetching Button as follows. Make sure that the TextView is public static
    Main.png

  • Now create a new Java Class as follows
    new class.png

  • And name it in my case I named it FetchMyData

new class1.png


  • Now open this Link and paste your JSON data here or write down a new and save it.
    json1.png

  • Save the data you will provide an API link. Copy this API Link we will use it later.
    json3.png
  • Now extends you FetchMyData.java class and write down the code as follows
    new111.png


  • You can also copy this code and change the Activity name According to yours.


public class FetchMyData extends AsyncTask<Void,Void,Void> {
String result;
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://api.myjson.com/bins/.....");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = bufferedReader.readLine();
result = result + line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.mydata.setText(result);
}
}


  • Now open the Mainfest and add Internet Permission as follows
    mianfest.png

    <uses-permission android:name="android.permission.INTERNET"/>


  • Now write down the code in OnClickListener on Button in MainActivity as follows
    maina.png

    fetchdata.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    // Logic Code here When this Button is Clicked
    FetchMyData process = new FetchMyData();
    process.execute();

          }
      });
    

    • Now run your app any of your installed Emulator with "shift+F10" or "F10" and test your app by clicking the Button in it.

    mob.png

    Thanks for Reading


    Upvote|Resteem|Follow

Sort:  

Immensely minimal :)

Hello, as a member of @steemdunk you have received a free courtesy boost! Steemdunk is an automated curation platform that is easy to use and built for the community. Join us at https://steemdunk.xyz

Upvote this comment to support the bot and increase your future rewards!

Congratulations @faisalamin! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Support SteemitBoard's project! Vote for its witness and get one more award!

@faisalamin, thank you for supporting @steemitboard as a witness.

Here is a small present to show our gratitude
Click on the badge to view your Board of Honor.

Once again, thanks for your support!

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.104
BTC 63427.77
ETH 1877.48
USDT 1.00
SBD 0.37