JSON Parsing in Android using
Asynctask
In Previous tutorial, mentioned about JSON types and
how JSON parsing is happening without using asynctask. In this tutorial, I will
talk about JSON parsing using Asynctask.
What
is Async Task?
AsyncTask enables proper and easy use of the UI thread.This class allows to perform background operations and publish results on the UI thread.
Async Task has Four Operations:
1. onPreExecute()- It invoked before execution.
2. onPostExecute()- It invoked after execution.
3. doInBackground()- Main operation, write your long operation code.
4. onPregressUpdate()- It indicates user on progress.
Async Task has Three generic types:
AsyncTask<Param, Progress, Result>
AsyncTask<{Type of Input},{Type of Update Unit},{Type of Update Result}>
1. onPreExecute()- It invoked before execution.
2. onPostExecute()- It invoked after execution.
3. doInBackground()- Main operation, write your long operation code.
4. onPregressUpdate()- It indicates user on progress.
Async Task has Three generic types:
AsyncTask<Param, Progress, Result>
AsyncTask<{Type of Input},{Type of Update Unit},{Type of Update Result}>
In this tutorial, I am taking an example of following
JSON which will give you list of News and each “NewsLines” have different Head-Lines
and Date-Lines. You can get this JSON by accessing http://mfeeds.timesofindia.indiatimes.com/Feeds/jsonfeed?newsid=3947071&amp;format=simplejson
If you observe carefully, you can see { and ]. The main
difference between these two is object and array. Curly bracket { represents JSONObject and Square bracket [
represents JSONArray.
In this example, I will create a button and after
clicking on button the data will show in listview. So our main.xml will be like
this:
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/getdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="Get Data" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/getdata"
android:layout_centerHorizontal="true"
>
</ListView>
</RelativeLayout>
Now we will create Parser
class. This parser class is used to execute the JSON link and get the JSON data
and returns JSON Object.
public class Parser {
static
InputStream is = null;
static
JSONObject jObj = null;
static String json = "";
public
JSONObject getJSONData(String url){
try{
DefaultHttpClient
client = new DefaultHttpClient();
HttpPost
post = new HttpPost(url);
HttpResponse
response = client.execute(post);
HttpEntity
entity = response.getEntity();
is =
entity.getContent();
}catch (UnsupportedEncodingException
e) {
e.printStackTrace();
} catch
(ClientProtocolException e) {
e.printStackTrace();
} catch
(IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new
BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new
StringBuilder();
String line = null;
while ((line =
reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json =
sb.toString();
} catch
(Exception e) {
Log.e("Buffer
Error", "Error converting result " +
e.toString());
}
try {
jObj = new JSONObject(json);
} catch
(JSONException e) {
Log.e("JSON
Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
In NewsLines, there are multiple strings i.e HeadLine and
DateLine. So you need to define these in your MainActivity class.
private static final String NEWS = "NewsML";
private static final String NEWS_LINE = "NewsLines";
private static final String HEAD_LINE = "HeadLine";
private static final String DATE_LINE = "DateLine";
After button click, it will execute AsyncTask, so you need to write like this way:
Btngetdata.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
new
JSONParse().execute();
}
});
Here JSONParse is a class for Asynctask operation.By using this JSONParse class, it will get the data and fill in the list view. So JSONPasre class will look like this:
private class JSONParse extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>>{
ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
private ProgressDialog pDialog;
protected void
onPreExecute() {
super.onPreExecute();
pDialog = new
ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading
Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected
ArrayList<HashMap<String, String>> doInBackground(String... params)
{
Parser jParser = new
Parser();
JSONObject json =
jParser.getJSONData(url);
try{
news =
json.getJSONArray(NEWS);
for(int
i=0;i<news.length();i++){
JSONObject
object = news.getJSONObject(i);
JSONObject
newsline = object.getJSONObject(NEWS_LINE);
String
headLine = newsline.getString(HEAD_LINE);
String
dateLine = newsline.getString(DATE_LINE);
HashMap<String,
String> map = new HashMap<String, String>();
map.put(HEAD_LINE,
headLine);
map.put(DATE_LINE,
dateLine);
newsList.add(map);
}
}catch(JSONException
e){
e.printStackTrace();
}
return newsList;
}//Ending doInBackground
@Override
protected void
onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
pDialog.dismiss();
String []from = {HEAD_LINE,DATE_LINE};
int []to =
{R.id.tv_headLn,R.id.tv_dateLn};
ListAdapter adapter = new
SimpleAdapter(MainActivity.this, result,R.layout.listview_item, from,
to);
setListAdapter(adapter);
}
So the MainActivity class will look like this:
MainActivity.java
public class
MainActivity extends ListActivity {
Button Btngetdata;
private static final String NEWS = "NewsML";
private static final String NEWS_LINE = "NewsLines";
private static final String HEAD_LINE = "HeadLine";
private static final String DATE_LINE = "DateLine";
JSONArray news = null;
//URL to get JSON Array
private static String url = "http://mfeeds.timesofindia.indiatimes.com/Feeds/jsonfeed?newsid=3947071&amp;format=simplejson";
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Btngetdata =
(Button)findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
new
JSONParse().execute();
}
});
}
private class
JSONParse extends AsyncTask<String, Integer,
ArrayList<HashMap<String, String>>>{
ArrayList<HashMap<String,
String>> newsList = new ArrayList<HashMap<String, String>>();
private
ProgressDialog pDialog;
protected void
onPreExecute() {
super.onPreExecute();
pDialog = new
ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading
Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected
ArrayList<HashMap<String, String>> doInBackground(String... params)
{
Parser jParser = new
Parser();
JSONObject json =
jParser.getJSONData(url);
try{
news =
json.getJSONArray(NEWS);
for(int
i=0;i<news.length();i++){
JSONObject
object = news.getJSONObject(i);
JSONObject
newsline = object.getJSONObject(NEWS_LINE);
String
headLine = newsline.getString(HEAD_LINE);
String
dateLine = newsline.getString(DATE_LINE);
HashMap<String,
String> map = new HashMap<String, String>();
map.put(HEAD_LINE,
headLine);
map.put(DATE_LINE,
dateLine);
newsList.add(map);
}
}catch(JSONException
e){
e.printStackTrace();
}
return newsList;
}
@Override
protected void
onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
pDialog.dismiss();
String []from = {HEAD_LINE,DATE_LINE};
int []to =
{R.id.tv_headLn,R.id.tv_dateLn};
ListAdapter adapter = new
SimpleAdapter(MainActivity.this, result, R.layout.listview_item, from,
to);
setListAdapter(adapter);
}
}
}
Result:
very helpful....thanku
ReplyDeleteDe donde sacas R.id.tv_headLn , R.id.tv_dateLn ?
ReplyDeleteGracias
Sorry I meant, where did you get R.id.tv_headLn , R.id.tv_dateLn ?
ReplyDeletevery nicely elucidated
ReplyDeleteI read this article got very useful and understandable for me through step by step description thanks for sharing.
ReplyDeleteInterview Cracker for Android
cool !
ReplyDeletenice article...very useful
ReplyDeletevery useful.....ty :)
ReplyDeletei am having warning that DefaultHttpClient is the deprecated code
ReplyDeletemadhavi laddha
ReplyDeleteHi, I Like Your Post…….
ReplyDeleteOn the last day of the month of Ramadan, Jumtat-ul-Vida is socialized by the Muslim community. Ramzan usually comes in the ninth month according to the Islamic calendar.
Thanks For Sharing The Information The Information Shared Is Very Valuable Please Keep Updating Us Time Just Went On Reading The article Python Online Course Hadoop Online Course Aws Online Course Data Science Online Course
ReplyDeleteMicrosoft Windows Azure Training | Online Course | Certification in chennai | Microsoft Windows Azure Training | Online Course | Certification in bangalore | Microsoft Windows Azure Training | Online Course | Certification in hyderabad | Microsoft Windows Azure Training | Online Course | Certification in pune