Click here to Skip to main content
14,210,255 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello

i've written asp.net web service in asp.net and return json format data

out put look like this

<string xmlns="http://tempuri.org/">
<script id="tinyhippos-injected"/>
[{"UserId":"D0009 ","Username":"V. Gopalakrishnan ","CustID":"122","CoName":"Paras Telecom Pvt.Ltd.","DbName":"ParEntSer86","LoginYn":"1","XmlPath":null,"FunctionID":"7","EmailId":"gopal@parastelecom.com","RoleId":null}]
</string>


and now i wnt to use this web service in android application and receive data so please help

how can this possible
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Here is how i used it was working fine

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
 

namespace Webservice
{
    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {
        public Service1()
        {
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        
        public Void GetEmployees()
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT *  FROM Contact e ";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.SelectCommand.Connection = con;
            da.Fill(dt);
            con.Close();
            
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;
            foreach (DataRow rs in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, rs[col]);
                }
                rows.Add(row);
            }
 
           this.Context.Response.ContentType = "application/json; charset=utf-8";
     this.Context.Response.Write(serializer.Serialize(new { Cargo = rows }));
        }     
 
        public string errmsg(Exception ex)
        {
            return "[['ERROR','" + ex.Message + "']]";
        }
    }
}


Quote:
Here is my output:(i'm not getting xml strings)


{"Cargo":[{"Id":1,"FirstName":"devi","LastName":"priya ","Contactno":"965577796 "},{"Id":2,"FirstName":"arun","LastName":"kumar","Contactno":"9944142109"},{"Id":3,"FirstName":"karu","LastName":"ronald","Contactno":"8883205008"}]}
   
Comments
Appdev(Icode) 19-May-14 9:35am
   
After creating webservices as above and you can use json parser to call webservice from android application..let me know if it helps.
Appdev(Icode) 19-May-14 9:37am
   
If you return with xml string you will get error in android emulator...so try to return the ans without the string/
Ajaaz2 4-Dec-14 7:35am
   
Hi. I tried your example but i am getting error at line -
this.Context.Response.Write(serializer.Serialize(new { Cargo = rows }));

What does it mean?
I have tried many examples to return data in json format but no success
Balaacrus 8-Apr-15 2:10am
   
Its work fine.Good answer.
shankar00772 26-Jul-15 9:38am
   
Cargo mean in result ?
biswajit mishra 9-Nov-17 6:13am
   
Thank you so much.It works for me.
Paul Yataco 10-Apr-18 19:13pm
   
cual es el return

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190618.1 | Last Updated 19 May 2014
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100