Sunday, September 28, 2014

Google Maps Android API v2 Key

In here i will explain how to generate android goggle map v2 API key for android application.First You have to generate SHA-1 finger print.For that you use terminal.


keytool -list -v -keystore C:\Users\<username>\.android\debug.keystore -storepass android -keypass android














Then you can see SHA-1 is printing.Then go to here(https://code.google.com/apis/console/?noredirect) and create new project.Then go to API access and click the create new android key button.Then add your sha-1 finger print;packagename and genarate key.

Ex :-AC:C7:99:64:B7:9B:C8:25:53:6D:B2:2F:79:59:59:F9:37:1B:AF:A4;com.example.appname

com.example.appname is your android project package name.Then you can see there is key generated.You can use this key for your application.

Monday, September 8, 2014

Android volley library json parse example

In this post i will describe how to use android volley library for your json request.First you have to download android volley library.I have uploaded this sample projcet to github so you can download volley.jar file from here(https://github.com/sajith4u/Android_volley_Sample/tree/master/Android_Volley/libs).Or you can download from here(https://drive.google.com/?tab=jo&authuser=0#folders/0B6boUJ62bUIXZUJQbUdoS08tMms).First You have to add AppController class to your package.Befor that make sure you have put volley.jar file in to your lib folder.





package com.example.Android_Volley;



import android.app.Application;

import android.text.TextUtils;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.toolbox.Volley;



/**

 * Created with IntelliJ IDEA.

 * User: Sajith

 * Date: 9/8/14

 * Time: 9:04 PM

 * To change this template use File | Settings | File Templates.

 */

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();



    private RequestQueue mRequestQueue;



    private static AppController mInstance;



    @Override

    public void onCreate() {

        super.onCreate();

        mInstance = this;

    }



    public static synchronized AppController getInstance() {

        return mInstance;

    }



    public RequestQueue getRequestQueue() {

        if (mRequestQueue == null) {

            mRequestQueue = Volley.newRequestQueue(getApplicationContext());

        }



        return mRequestQueue;

    }



    public <T> void addToRequestQueue(Request<T> req, String tag) {

        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);

        getRequestQueue().add(req);

    }



    public <T> void addToRequestQueue(Request<T> req) {

        req.setTag(TAG);

        getRequestQueue().add(req);

    }



    public void cancelPendingRequests(Object tag) {

        if (mRequestQueue != null) {

            mRequestQueue.cancelAll(tag);

        }

    }

}

Then You have to Add this class to your manifest file.So inside the Application tag put the name of class.


<application android:label="@string/app_name"

                 android:icon="@drawable/ic_launcher"

                 android:name="com.example.Android_Volley.AppController">

        <activity android:name="MyActivity"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>

            </intent-filter>

        </activity>

    </application>

And also make shure add internet permission in to your application.



 <uses-permission android:name="android.permission.INTERNET"/>

Then inside main activity i have one button and when i click button it will read json object and show in to textview.I have used this public json call to show demo.(http://time.jsontest.com/) it will return date, time and other parameter inside json object.

date:- String
mili seconds :- Double
time:- String

when button clicked it called to this method.



   /**
     *  Send request
     */
    private void makeJsonObjectRequest() {

        showpDialog();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
                urlJsonObj, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                try {
                    // Parsing json object response
                    // response will be a json object
                    String time = response.getString("time");
                    Double mili = response.getDouble("milliseconds_since_epoch");
                    String date = response.getString("date");


                    jsonResponse = "";
                    jsonResponse += "Time: " + time + "\n\n";
                    jsonResponse += "seconds: " + mili + "\n\n";
                    jsonResponse += "Date: " + date + "\n\n";


                    txtResponse.setText(jsonResponse);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
                hidepDialog();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hidepDialog();
            }
        });
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }


In this method hideDialog and showDialog methods used to add progressDialog.



Tuesday, August 19, 2014

Android Httpclient with Json

In this Tutorial i will describe how to use android HTTP client to get result from json request.For that i have used this public json API to get results.(http://date.jsontest.com/).

Here you can see three respond data from this json api.