This tutorial describes how to use JSON with Android. JSON stands for (Java Script Object Notation). It is a simple and light-weight data interchange format that can be easily read by humans and machines. JSON is a text format that is language independent. It represents data in a text format so that can be easily parsed.
Introduction to JSON
JSON uses two different of structures:
-> Collection of name/value pair
-> Array
Lets take the api from the Android hive:
http://api.androidhive.info/contacts/
Creating New Android Project:
public static ArrayList<HashMap<String, String>> JsonResponseUrl(
String url) throws JSONException{
arraylist =new ArrayList<HashMap<String,String>>();
JSONArray json_array = new JSONArray(url);
for (int i = 0; i < json_array.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = json_array.getJSONObject(i);
map.put("sms", jsonobject.getString("contacts"));
arraylist.add(map);
}
return arraylist;
}
:Http Connection:
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null; int statusCode = 0;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
statusCode = response.getStatusLine().getStatusCode();
Log.d("Http Request------------------>", ""+statusCode);
if(statusCode == 200)
{
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
}
} finally {
Log.d("Http Request1------------------>", ""+statusCode);
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
Implement on AsyncTask:
class ProgressBar_History extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
history_url = String.format("http://api.androidhive.info/contacts/");
history_url1 = Utility.urlEncode(history_url);
try {
Log.d(" convert url ----------------->", ""+history_url1);
url =executeHttpGet(history_url1);
Log.d("url responce from JSON----------------->", ""+url);
if(url != null)
{
arraylist_history = jsonhistoryResponseUrl(url);
}else{
mProgressDialog.dismiss();
Log.d("Server Error", "");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog = ProgressDialog.show(getActivity(),"history", "Please Wait...",true);
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
mProgressDialog.dismiss();
if(arraylist_history != null){
adapter = new HistoryListAdapter(getActivity(), R.layout.row_smshistory_list, arraylist_history);
history_list.setAdapter(adapter);
}
}
}
Introduction to JSON
JSON uses two different of structures:
-> Collection of name/value pair
-> Array
Lets take the api from the Android hive:
http://api.androidhive.info/contacts/
{ "contacts" : [ { "id" : "c200" , "name" : "Ravi Tamada" , "email" : "ravi@gmail.com" , "address" : "xx-xx-xxxx,x - street, x - country" , "gender" : "male" , } . ] } |
Creating New Android Project:
webservice Function:
public static ArrayList<HashMap<String, String>> JsonResponseUrl(
String url) throws JSONException{
arraylist =new ArrayList<HashMap<String,String>>();
JSONArray json_array = new JSONArray(url);
for (int i = 0; i < json_array.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = json_array.getJSONObject(i);
map.put("sms", jsonobject.getString("contacts"));
arraylist.add(map);
}
return arraylist;
}
:Http Connection:
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null; int statusCode = 0;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
statusCode = response.getStatusLine().getStatusCode();
Log.d("Http Request------------------>", ""+statusCode);
if(statusCode == 200)
{
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
}
} finally {
Log.d("Http Request1------------------>", ""+statusCode);
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
Implement on AsyncTask:
class ProgressBar_History extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
history_url = String.format("http://api.androidhive.info/contacts/");
history_url1 = Utility.urlEncode(history_url);
try {
Log.d(" convert url ----------------->", ""+history_url1);
url =executeHttpGet(history_url1);
Log.d("url responce from JSON----------------->", ""+url);
if(url != null)
{
arraylist_history = jsonhistoryResponseUrl(url);
}else{
mProgressDialog.dismiss();
Log.d("Server Error", "");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog = ProgressDialog.show(getActivity(),"history", "Please Wait...",true);
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
mProgressDialog.dismiss();
if(arraylist_history != null){
adapter = new HistoryListAdapter(getActivity(), R.layout.row_smshistory_list, arraylist_history);
history_list.setAdapter(adapter);
}
}
}
No comments:
Post a Comment