Android Json parsing Example with listview?



The process of parsing JSON in Android is pretty simple, thankfully. We'll be using JSONObject for all the parsing goodness - there are also some other JSON classes, but we'll just go through the basic ones to give you a general idea of how it works.

Activity.Java


public class AndroidJSONParsingActivity extends Activity {
private ProgressDialog dialog;
private String string,id,name,mail;  
private ListView listview;
private ArrayList<SearchParent> arrayParents;
//private ArrayList<String> arrayChildren; 

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new ProgressDialog(this);
string = "http://api.androidhive.info/contacts/";
 
listview = (ListView) findViewById(R.id.listView);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
//   Auto-generated method stub
Object  ob = (Object) view.getTag();  
if(ob != null) {
TotalhistoryInfo parent = (TotalhistoryInfo) view.getTag();  
String name = parent.getPage_name();  
String mail = parent.getPage_date();
String ID = parent.getId();
Log.i("details=>", ID + name + mail);  
}
}
});
new uploadaudio().execute();
}
private void ProgressBar_show() {

((ProgressDialog) dialog).setTitle("JsonParsing");  
((ProgressDialog) dialog).setMessage("Your content is loading.. \nPlease wait..");
((ProgressDialog) dialog).setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}

private void ProgressBar_hide() {
dialog.dismiss();
}
class uploadaudio extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressBar_show();
}

@Override
protected String doInBackground(String... params) {
load_JsonSections();
return null;
}
@SuppressLint("SetJavaScriptEnabled")
protected void onPostExecute(String result) {  
super.onPostExecute(result);
 
Log.i("onPostExecute==> ", arrayParents.toString());
AddedHistoryAdapter adapter = new AddedHistoryAdapter(AndroidJSONParsingActivity.this, R.layout.row_acc_info, arrayParents );   
listview.setAdapter(adapter); 
ProgressBar_hide();  
}
}
  
private void load_JsonSections() {
arrayParents = new ArrayList<SearchParent>();  
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(string);
HttpResponse response;
try {
response = httpclient.execute(httpget);  
HttpEntity entity = response.getEntity();  

if (entity != null) {

InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
JSONObject json = new JSONObject(result);
 
JSONArray jsonOnb = json.getJSONArray("contacts");  
for (int i = 0; i < jsonOnb.length(); i++) {
JSONObject _Jsonquery = jsonOnb.getJSONObject(i);  

if (_Jsonquery.has("id")) {
 
id = _Jsonquery.getString("id");
name = _Jsonquery.getString("name");
mail = _Jsonquery.getString("email");  
arrayParents.add(new SearchParent(name, mail, id));  
}  
}
instream.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}

private String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}

AdapterActivity.java



public class AddedHistoryAdapter extends ArrayAdapter<SearchParent> {
//private final static String TAG = AccountsAdapter.class.getSimpleName();
private ArrayList<SearchParent> _valu;
private Context context;

public AddedHistoryAdapter(Context _context, int _names, ArrayList<SearchParent> valu) {
super(_context, R.layout.row_acc_info, valu);
context = _context;
_valu = valu;
Log.i("_value= ", _valu.toString());
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
//View view = convertView;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_acc_info, parent, false);
}

TextView acc_id = (TextView) convertView.findViewById(R.id.textView_id);  
TextView acc_name = (TextView) convertView.findViewById(R.id.textView1);  
TextView acc_date = (TextView) convertView.findViewById(R.id.textView2);  
 
SearchParent acc_info = _valu.get(position);    
String name = acc_info.getName();
String date = acc_info.getMail();
String ID = acc_info.getId();
acc_name.setText(name);
acc_date.setText(date);
acc_id.setText(ID); 
TotalhistoryInfo total_acc = new TotalhistoryInfo();
total_acc.setPage_name(name);
total_acc.setPage_date(date);
total_acc.setId(ID);
convertView.setTag(total_acc);  
return (convertView);
}  
}

SearchParent.java


public class SearchParent {  
private String Id;
private String name;
private String mail;
public String getId() {
return Id;
}
public void setId(String id) {
this.Id = id;
}
public SearchParent(String i, String d, String p) {
this.mail = d;
this.name = i;
this.Id = p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}

TotalHistoryInfo.java


public class TotalhistoryInfo {
private String id;
private String page_name;
private String page_date;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPage_date() {
return page_date;
}
public void setPage_date(String page_date) {
this.page_date = page_date;
}
public String getPage_name() {
return page_name;
}
public void setPage_name(String acc_info) {
this.page_name = acc_info;
}
}

No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...