1

Im trying to get details like. District, name, Id ect... from MsSql DB through .Net web Service from a array object.

My web Service

[WebMethod]

public DisAndPanDetails[] GetDistrictNameDetails()
{
    DisAndPanDetails[] objDetails = new DisAndPanDetails[1];
    SQLHelper objHelper = new SQLHelper();
    DataTable dtblDetails = new DataTable();
    string sQuery = string.Empty;

    try
    {
        sQuery = "SELECT DISTINCT District FROM OnlineTaxMerchantIdDetails ORDER BY District";

        objHelper.CreateConnection("Connect");
        dtblDetails = objHelper.FillDataTableByQueryString(sQuery);

        if (dtblDetails.Rows.Count > 0)
        {
            DisAndPanDetails[] objDetail = new DisAndPanDetails[dtblDetails.Rows.Count];

            for (int iRowIdx = 0; iRowIdx < dtblDetails.Rows.Count; iRowIdx++)
            {
                objDetail[iRowIdx] = new DisAndPanDetails();
                objDetail[iRowIdx].District = dtblDetails.Rows[iRowIdx]["District"].ToString();
            }
        }   
    }
    catch (Exception ex)
    {
        objDetails[0] = new DisAndPanDetails();
        objDetails[0].Error = ex.Message.ToString();
    }
    finally
    {
        objHelper = null;
        dtblDetails = null;
    }

    return objDetails;
}

My KvmSerializable class

 public class DisAndPanDetails implements KvmSerializable 
 {

public String District;


public DisAndPanDetails()
{

}

public DisAndPanDetails(String District)
{
    this.District= District;
}

public Object getProperty(int arg0) {
    // TODO Auto-generated method stub
    switch(arg0)
    {
    case 0:
        return District;
    }
   // return null;
    return null;
}

public int getPropertyCount() {
    // TODO Auto-generated method stub
    return 1;
}

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    // TODO Auto-generated method stub
    switch(index)
    {
    case 0:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "District";
        break;
    default:
        break;
    }
}

public void setProperty(int index, Object value) {
    // TODO Auto-generated method stub
    switch(index)
    {
    case 0:
        District = value.toString();
        break;

    default:
        break;
    }
  }

My Activity Class

   DisAndPanDetails C = new DisAndPanDetails();

    PropertyInfo pi = new PropertyInfo();
    pi.setName("C");
    pi.setValue(C);
    pi.setType(C.getClass());
    Request.addProperty(pi);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(Request);

    envelope.addMapping(NAMESPACE, "DisAndPanDetails",new DisAndPanDetails().getClass());
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try
    {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response = (SoapObject)envelope.getResponse();
        C.District =  response.getProperty(0).toString();
        System.out.println("Result yash= Dist: "+C.District1+" count:"+intPropertyCount);
        }
        KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
        for(int i=0;i<ks.getPropertyCount();i++)
        {
        String obj[] =  soap.getProperty(0).toString();
        String values[] = (String[])Request.getAttribute("District1");
        }
        System.out.println("rrrr"+response);

        TextView tv = (TextView)findViewById(R.id.text123);
        tv.setText(C.District); 
        System.out.println("kkkk"+C.District);
        }
    catch(Exception e)
    {
        e.printStackTrace();

        System.out.println("error:"+e);
    }
    }

I need to get all district ,name, id into spinner. I can bind it to spinner but how can I get those detail of array obj as string from web service. Pls do help.

8
  • Why don't you have web service give you output in Tags, which means you don't write queries in your parsing code. You only get those tags in Array using Bean class(Get and Set methods). Then fill that array in Spinner using adapter(BaseAdapter preferred). Commented Oct 29, 2013 at 5:53
  • It's an existing web service. No privilege for me to modify the web service. I can only get the service. I need to see How can I make use of it. And data is stored in Mssql DB. Commented Oct 29, 2013 at 6:03
  • Is your service invokable? Does it give XML output? Because Service can call related query from its .NET code and give you result in tags. This is a swift way of doing it. For better performance you shouldn't sort or process web service output. Make sure you get only what you need. Make different services if there are different queries. I have experience with this approach and believe me, it's way better. Commented Oct 29, 2013 at 6:06
  • yes it is. etownpanchayat.com/JavaService/Service.asmx Commented Oct 29, 2013 at 6:12
  • Check this : androidexample.com/Dot_Net_Webservice_Call_-_Android_Example/…. By invokable i mean like in the link. It has textBoxes to obtain values and give you output. By the way, you can know what you will get and prepare your code to receive. Commented Oct 29, 2013 at 6:23

1 Answer 1

1

All right this is lengthy. Take it step by Step

1. Create a new class for Web Service Requests:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.content.Context;
import android.util.Log;

public class WebRequestManager {

    // YOUR Web Services
    public static final String LOGIN_METHOD = "///YOUR WEB SERVICE NAME";

    // ----------------------------------------------//
    //----SET BELOW THINGS ACCORDING TO YOUR NEED-----//

    public static final String NAMESPACE = "http://microsoft.com/webservices/";
    public static final String SOAP_ACTION = "http://microsoft.com/webservices/";
    public static final String URL = "http://1**.5*.1**.1**/test/WEB_SERVIVE.asmx";

    private SoapObject response = null;

    public WebRequestManager(Context context) {
    }

    public void sendRequest(SoapObject requestObj, String methodName) {

        try {

            SoapSerializationEnvelope mySoapEnvelop = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            // request.addProperty("sError", "");

            AndroidHttpTransport transport = new AndroidHttpTransport(URL);

            mySoapEnvelop.setOutputSoapObject(requestObj);
            mySoapEnvelop.dotNet = true;
            transport.call(SOAP_ACTION + methodName, mySoapEnvelop);

            response = getPureResponseObject((SoapObject) mySoapEnvelop.bodyIn);



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SoapObject getResponse() {
        return this.response;
    }

    private SoapObject getPureResponseObject(SoapObject result) {
        result = (SoapObject) result.getProperty(0);
        result = (SoapObject) result.getProperty(1);
        result = (SoapObject) result.getProperty(0);
        return result;
    }
}

** 2. CREATE A BEAN CLASS WITH STRINGS FROM YOUR WEB SERVICE RESPONSE**

package com.*.*;

public class Customer {
    String customerTypeId;
    String customerType;
    String sDARCustomer;

    // BEAN CLASS FOR  Web service

    public String getCustomerTypeId() {
        return customerTypeId;
    }

    public void setCustomerTypeId(String customerTypeId) {
        this.customerTypeId = customerTypeId;
    }

    public String getCustomerType() {
        return customerType;
    }

    public void setCustomerType(String customerType) {
        this.customerType = customerType;
    }

    public String getsDARCustomer() {
        return sDARCustomer;
    }

    public void setsDARCustomer(String sDARCustomer) {
        this.sDARCustomer = sDARCustomer;
    }

}

3. CREATE AN INDEPENDENT PARSER CLASS

public static ArrayList<BEAN CLASS> parseGetCustomerTypeResponse(
            SoapObject response) {
        ArrayList<BEAN CLASS> customerList = new ArrayList<BEAN CLASS>();
        for (int count = 0; count < response.getPropertyCount(); count++) {

            Customer customer = new Customer();
            SoapObject records = (SoapObject) response.getProperty(count);

            // BELOW SET YOUR set METHODS FROM BEAN CLASS
            customer.setCustomerTypeId(records.getProperty(
                    "YOUR WEB TAG NAME").toString());
            customer.setCustomerType(records.getProperty("YOUR WEB SERVICE TAG NAME")
                    .toString());

            customerList.add(customer);
        }

        return customerList;

    }

** 4. FOLLOWING CODE IN YOUR ACTIVITY. REMEMBER TO CHANGE THINGS IN ACCORDANCE WITH NAMES OF YOUR ADAPTERS AND METHODS **

class GetCustomerType extends AsyncTask<String, Void, Integer> {

        private ProgressDialog progress;

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(GetCustomerTypeActivity.this, "",
                    "Loading ...");
        }

        @Override
        protected Integer doInBackground(String... params) {


                try {
                    WebRequestManager requestManager = new WebRequestManager(
                            GetCustomerTypeActivity.this);
                    SoapObject request = new SoapObject(
                            WebRequestManager.NAMESPACE,
                            WebRequestManager.METHOD NAME FROM REQUEST CLASS);
                    requestManager.sendRequest(request,
                            WebRequestManager.METHOD NAME FROM REQUEST CLASS);

                // DECLARE ARRAYLIST & ARRAYADAPTER OUTSIDE OF ASYNC TASK. IT IS HERE FOR REFERENCE ONLY
                    ArrayList<Customer> custArrList;
                    CustomerArrayAdapter adpater;
                // BELOW CODE WILL CALL THE PARSER METHOD. CHANGE NAMES ACCORDINGLY

                    custArrList = Parsers
                            .parseGetCustomerTypeResponse(requestManager
                                    .getResponse());
                    return Util.SUCCESS;
                } catch (Exception e) {
                    e.printStackTrace();

                }

        }

        protected void onPostExecute(Integer result) {

                progress.dismiss();
                           //SET YOUR OWN ADAPTER HERE FOR SPINNER
        adpater = new CustomerArrayAdapter(
                GetCustomerTypeActivity.this,
                R.layout.get_customer_type_imagerow,custArrList);
                custTypeList.setAdapter(adpater);


            progress.dismiss();

        }

    }

IN THE ACTIVITY PUT THE ADAPTER CODE LIKE BELOW :

class CustomerArrayAdapter extends BaseAdapter {

        private Context context;
        private int resID;
        private ArrayList<BEAN CLASS> items;

        public CustomerArrayAdapter(Context context, int resID,
                ArrayList<BEAN CLASS> items) {
            this.context = context;
            this.resID = resID;
            this.items = items;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View row, ViewGroup parent) {

            if (row == null) {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(resID, null);
            }

            BEAN CLASS item = items.get(position);

            // THIS IS FOR CUSTOM LISTVIEW. CHANGE NAMES AS NECESSARY
            TextView custType = (TextView) row
                    .findViewById(R.id.textViewGetCust);
            custType.setText(item.getCustomerType());
            return row;

            // FOR SPINNER CHANGE ABOVE CODE AS BELOW

            BEAN CLASS item = items.get(position);
            TextView text = (TextView) row.findViewById(android.R.id.text1);
            text.setText(item.getCityName());
            text.setTextSize(20);
            return row;
        }

    }
Sign up to request clarification or add additional context in comments.

2 Comments

I resolved it in another way around bit easier one. And thanks for the Help though. I took an idea from ur post for another purpose.
Happy to help anyhow. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.