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

Words
423
Reading
2 min
Listen
Play
8y

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

How to fetch data from a JSON file to Android using the URL with An... | Ecency