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.
- My GitHub Profile: https://github.com/rockstar96969696
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.
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"
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
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
Now create a new Java Class as follows
And name it in my case I named it FetchMyData
- Now open this Link and paste your JSON data here or write down a new and save it.
- Save the data you will provide an API link. Copy this API Link we will use it later.
- Now extends you FetchMyData.java class and write down the code as follows
- 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
<uses-permission android:name="android.permission.INTERNET"/>
Now write down the code in OnClickListener on Button in MainActivity as follows
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.
Thanks for Reading
Upvote|Resteem|Follow
Immensely minimal :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @faisalamin! You have received a personal award!
1 Year on Steemit
Click on the badge to view your Board of Honor.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@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!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit