To day I will show you how to access Asp.net web service that's return data in Json (Java Script object notation ) Data Format.
I have already shared how to make web service on asp.net.
I also shared how to deploy asp.net service on local host and how to access deployed web service on Android browser .
I also shared how to access asp.net 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.
Dummy Data :
And After Deploy when you run this Service It look Like
My web service URL is http://119.157.137.226/VirtualTounge/WebService1.asmx
Now we Move on Android :-
Create A Project and Write Code in Activity File :-
Android Main.xml
Download Source Code:-
1 ) Source 1 (File4Shared)
2 ) Source 2 (Google Docs)
I have already shared how to make web service on asp.net.
I also shared how to deploy asp.net service on local host and how to access deployed web service on Android browser .
I also shared how to access asp.net 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.
Data Structure of Table: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 = "http://tempuri.org/")] [WebServiceBindingundefinedConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItemundefinedfalse)] // 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 { [WebMethod] public string VisualStudioLearnundefinedstring InputName) { return "Welcome "+InputName; } [WebMethod] public string GetDataJSONundefined) { dbcon db = new dbconundefined); DataSet r = db.GetDataSetundefined"select * from tblUsers"); string s = JsonConvert.SerializeObjectundefinedr); return s; } } }
Dummy Data :
And After Deploy when you run this Service It look Like
My web service URL is http://119.157.137.226/VirtualTounge/WebService1.asmx
Now we Move on Android :-
Create A Project and Write Code in Activity File :-
Android Manifest 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.app.Activity; 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; @Override public void onCreateundefinedBundle savedInstanceState) { super.onCreateundefinedsavedInstanceState); setContentViewundefinedR.layout.main); songsList = new ArrayList<HashMap<String, String>>undefined); list=undefinedListView)findViewByIdundefinedR.id.list); String Result=GetJsonDataundefined); ifundefinedResult.equalsIgnoreCaseundefined"Data Loaded Sucessfully...")) { adapter=new LazyAdapterundefinedthis, songsList); list.setAdapterundefinedadapter); Toast.makeTextundefinedthis, "List is Rander now" ,Toast.LENGTH_LONG).showundefined); } else { 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) { @Override public void onItemClickundefinedAdapterView<?> parent, View view, int position, long id) { } }); } public String GetJsonDataundefined) { String SOAP_ACTION_GetJsonData ="http://tempuri.org/GetDataJSON"; String OPERATION_NAME_GetJsonData = "GetDataJSON"; String WSDL_TARGET_NAMESPACE ="http://tempuri.org/"; String SOAP_ADDRESS ="http://119.157.178.86/VirtualTounge/WebService1.asmx"; Object response=null; try { SoapObject request = new SoapObjectundefinedWSDL_TARGET_NAMESPACE,OPERATION_NAME_GetJsonData); SoapSerializationEnvelope envelope = new SoapSerializationEnvelopeundefined SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObjectundefinedrequest); 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)); //map.putundefinedKEY_Image_Url,"http://sphotos-e.ak.fbcdn.net/hphotos-ak-ash3/644597_618821581477731_745995561_n.jpg"); songsList.addundefinedmap); } response="Data Loaded Sucessfully..."; } else { response="NO Data is There"; } } catch undefinedException exception) { response="error: "+exception.toStringundefined); exception.printStackTraceundefined); //Toast.makeTextundefinedthis, exception.printStackTraceundefined) ,Toast.LENGTH_LONG).showundefined); } return ""+response.toStringundefined); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidhive" android:versionCode="1" android:versionName="1.0"> <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"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Android Main.xml
Sample Out Put :-<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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>
Download Source Code:-
1 ) Source 1 (File4Shared)
2 ) Source 2 (Google Docs)
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.
ReplyDelete"awan" pic android code.
thanks in advance
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 .
Delete