Subscribe For Free Updates!

We'll not spam mate! We promise.

May 19, 2013

Access J-son Web service on Android


To day I will show you how to access web service that's return data in Json (Java Script object notation )  Data Format.

I have already shared how to make web service on

I also shared how to deploy service on local host and how to access deployed web service on Android browser .

I also shared how to access XML Service on android , so must have to go through all these tutorials before reading this tutorial.

Json service are better the XML web service because,
Json service is Light weight, fast and don't cray any extra data like XML start and end Tags tags.

So Lets Start,
I already Shared Link How to Return Json data from .net web service.
Now Create a Web service having Following Code and Also deploy on IIS service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Data;

namespace WebApplication1
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebServiceundefinedNamespace = "")]
    [WebServiceBindingundefinedConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService

        public string VisualStudioLearnundefinedstring InputName)
         return "Welcome "+InputName;

        public string GetDataJSONundefined)
          dbcon db = new dbconundefined);
          DataSet r = db.GetDataSetundefined"select * from tblUsers");
          string s = JsonConvert.SerializeObjectundefinedr); 
          return s;
Data Structure of Table:

Dummy Data :

And After Deploy when you run this Service It look Like
My web service URL is

Now we Move on Android :-
Create A Project and Write Code in Activity File :-
package com.example.androidhive;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class CustomizedListView extends Activity {
 // All static variables
 // XML node keys
 static final String KEY_Parent_tag = "Users"; // parent node
 static final String KEY_SNO = "SNO";
 static final String KEY_Full_Name = "FullName";
 static final String KEY_UserName = "UserName";
 static final String KEY_Gender = "Gender";
 static final String KEY_Image_Url = "ImageUrl";
 ListView list;
    LazyAdapter adapter;
    ArrayList<HashMap<String, String>> songsList;

 public void onCreateundefinedBundle savedInstanceState) {

  songsList = new ArrayList<HashMap<String, String>>undefined);

  String Result=GetJsonDataundefined);
  ifundefinedResult.equalsIgnoreCaseundefined"Data Loaded Sucessfully..."))
    adapter=new LazyAdapterundefinedthis, songsList);        
          Toast.makeTextundefinedthis, "List is Rander now" ,Toast.LENGTH_LONG).showundefined);
   Toast.makeTextundefinedthis, "There Is no Recorde" ,Toast.LENGTH_LONG).showundefined);
  // Getting adapter by passing xml data ArrayList

        // Click event for single list row
        list.setOnItemClickListenerundefinednew OnItemClickListenerundefined) {

   public void onItemClickundefinedAdapterView<?> parent, View view,
     int position, long id) {

 public String GetJsonDataundefined)
  String SOAP_ACTION_GetJsonData ="";
  String OPERATION_NAME_GetJsonData = "GetDataJSON";
  String SOAP_ADDRESS ="";
  Object response=null;
   SoapObject request = new SoapObjectundefinedWSDL_TARGET_NAMESPACE,OPERATION_NAME_GetJsonData);
   SoapSerializationEnvelope envelope = new SoapSerializationEnvelopeundefined
   envelope.dotNet = true;
      HttpTransportSE httpTransport = new HttpTransportSEundefinedSOAP_ADDRESS);
   httpTransport.callundefinedSOAP_ACTION_GetJsonData, envelope);
   response = envelope.getResponseundefined);
   JSONObject mainJson = new JSONObjectundefinedresponse.toStringundefined));
   ///JSONArray jsonArray = mainJson.getJSONArrayundefined"student");
   ifundefinedresponse.toStringundefined).lengthundefined)>2 )
    JSONArray aryJSONStrings = mainJson.getJSONArrayundefinedKEY_Parent_tag);//new JSONArrayundefined EntityUtils.toStringundefinedundefinedHttpEntity) envelope.getResponseundefined)));
    int SNO=0;
    for undefinedint i=0; i<aryJSONStrings.lengthundefined); i++) {
     HashMap<String, String> map = new HashMap<String, String>undefined);
     SNO= aryJSONStrings.getJSONObjectundefinedi).getIntundefinedKEY_SNO);
     map.putundefinedKEY_SNO, SNO+"");
     map.putundefinedKEY_Full_Name, aryJSONStrings.getJSONObjectundefinedi).getStringundefinedKEY_Full_Name));
     map.putundefinedKEY_UserName, aryJSONStrings.getJSONObjectundefinedi).getStringundefinedKEY_UserName));
     map.putundefinedKEY_Gender, aryJSONStrings.getJSONObjectundefinedi).getStringundefinedKEY_Gender));
     map.putundefinedKEY_Image_Url, aryJSONStrings.getJSONObjectundefinedi).getStringundefinedKEY_Image_Url));
    response="Data Loaded Sucessfully...";
    response="NO Data is There";

  catch undefinedException exception)
   response="error: "+exception.toStringundefined);
   //Toast.makeTextundefinedthis, exception.printStackTraceundefined) ,Toast.LENGTH_LONG).showundefined);
  return ""+response.toStringundefined);

Android Manifest File :-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
 package="com.example.androidhive" android:versionCode="1"
 <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <application android:icon="@drawable/icon" android:label="@string/app_name">
  <activity android:name=".CustomizedListView" android:label="@string/app_name">
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />


Android Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#b5b5b5" android:dividerHeight="1dp" android:listSelector="@drawable/list_selector" /> </LinearLayout>
Sample Out Put :-

Download Source Code:-
1 ) Source 1 (File4Shared)
2 ) Source 2 (Google Docs)

Plese Feel Free to Socializer This Post


  1. hi sajjad Ashraf ,can you give me the link of your above android code where you changed the string output in to a textview. i am also trying but it is not happening.
    "awan" pic android code.
    thanks in advance

    1. First picture is sample picture it not part of project .purpose of this Tutorial how to Return Json,How to parse ,how to Bind it on List and Also How to show Images from URL in List view .

  2. If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you have to watch this video
    right away...

    (VIDEO) Win your ex back with TEXT messages?

  3. Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
    iPhone app training course in bangalore
    Mobile app training institutes bangalore


Become a Fan

visual studio learn