Saturday 29 November 2014

Android Json Tutorial

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

json parsing structor

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);

}
}
}




















Thursday 27 November 2014

Android GPS, Location Manager Tutorial

One of the unique features of mobile applications is location awareness. Mobile users take their devices with them everywhere, and adding location awareness to your app offers users a more contextual experience. The location APIs available in Google Play services facilitate adding location awareness to your app with automated location tracking, geofencing, and activity recognition.
The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.
This class shows you how to use the Google Play services location APIs in your app to get the current location, get periodic location updates, look up addresses, create and monitor geofences, and detect user activities. The class includes sample apps and code snippets that you can use as a starting point for adding location awareness to your app.

If you are developing any location based or map application, you can make your app more smarter by finding user’s location automatically. For this you need to integrate GPS modules in your application. In this tutorial i explained how to work with GPS / Location API.