Wednesday 20 August 2014

Android JSON Parsing Tutorial

Example for JSON String
--------------------------------------------

[
{
    "title": "School",
    "image": "jpg",
    "url": "http://",
 
    "State": "ddddd",
    "Phone": [
         "2",
        "4",
        "5",
    ]
},
{
    "title": "School",
    "image": "jpg",
    "url": "http://",

    "State": "ddddd",
    "phone": [
        "2",
        "4",
        "5",
    ]
}

]



----------------------------------------------------------


Where { } Represent JSONObject and [ ] repesents JSONArray


Java Code
-----------------------------

 String strJson="** THE ABOVE JSON STRING **";


OR If your Json String is in an URL then

ServiceHandler sh = new ServiceHandler();

// Making a request to url and getting response
String strJson = sh.makeServiceCall(url, ServiceHandler.GET);

              JSONArray mainArray = new JSONArray(strJson);

              for (int i = 0; i < mainArray.length(); i++) {
                  JSONObject sub = mainArray.getJSONObject(i);

                  String id = sub.getString("title");


                  String image = sub.getString("image");
                  String url=sub.getString("url");
                

                 

                  String State=sub.getString("State");

                  JSONArray genreArray=sub.getJSONArray("genre");

                  String genre1=genreArray.get(0).toString();
                  String genre2=genreArray.get(1).toString();
                  String genre3=genreArray.get(2).toString();
                


                  System.out.println("Id: "+id);
                  System.out.println("Image: "+image);
                  System.out.println("url: "+url);
               
               
                  System.out.println("State: "+State);
                  System.out.println("genre1: "+genre1);
                  System.out.println("genre2: "+genre2);
                  System.out.println("genre3: "+genre3);
                


              }


Tuesday 19 August 2014

Admob using Google play services for Banner Ads.

Create a layout in your xml file
-------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


<LinearLayout
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>



</LinearLayout>

In your Activity Page

---------------------------------------------


 AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        LinearLayout layout = (LinearLayout) findViewById(R.id.reklam);
       
layout.setVisibility(View.GONE);

        // Request for Ads
        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .build();

    // Start loading the ad in the background.
      adView.loadAd(adRequest);

      adView.setAdListener(new AdListener() {

                @Override
                public void onAdClosed() {
                    // TODO Auto-generated method stub
                    super.onAdClosed();
                }


                @Override
                public void onAdLoaded() {
                    // TODO Auto-generated method stub
                    super.onAdLoaded();
                    layout.addView(adView);
                    layout.setVisibility(View.VISIBLE);

                }


                @Override
                public void onAdOpened() {
                    // TODO Auto-generated method stub
                    super.onAdOpened();
                }

            });



Wednesday 23 July 2014

Convert HashSet to Arraylist in Android

Set<String> set = new HashSet<String>();
List<String> sample=new ArrayList<String>(set);

Convert Arraylist to HashSet Android

Set<String> set = new HashSet<String>();
set.addAll(your Arraylist name);

Store and Retrieve Array list using shared preferences in Android

Store Arraylist Using  Shared Preferences
------------------------------------------------------

SharedPreferences prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
Editor edit=prefs.edit();

Set<String> set = new HashSet<String>();
set.addAll(your Arraylist Name);
edit.putStringSet("yourKey", set);
edit.commit();

Retrieve Arraylist from Shared Preferences
-----------------------------------------------------

Set<String> set = prefs.getStringSet("yourKey", null);
List<String> sample=new ArrayList<String>(set);

Tuesday 22 July 2014

Sorting ArrayList in Android

Sorting Ascenting
----------------------------

Collections.sort(arraylist Name);


Sorting Descending
----------------------------------

Collections.sort(arraylist Name,Collections.reverseOrder());

Thursday 17 July 2014

android.app.RemoteServiceException

in android.app.ActivityThread$H.handleMessage
[10:59:21 AM] Manu Mohan: android.app.RemoteServiceException: Bad notification posted from package *************: Couldn't create icon: StatusBarIcon(pkg=**********user=0 id=0x7f02007b level=0 visible=true num=0 )
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)







Make sure the image or drawables specified in R.java with the given id is in all drawable folders.