Get Array Item From Webservice Object To Android
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[] GetDistric
Solution 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;
publicclassWebRequestManager {
// YOUR Web ServicespublicstaticfinalStringLOGIN_METHOD="///YOUR WEB SERVICE NAME";
// ----------------------------------------------////----SET BELOW THINGS ACCORDING TO YOUR NEED-----//publicstaticfinalStringNAMESPACE="http://microsoft.com/webservices/";
publicstaticfinalStringSOAP_ACTION="http://microsoft.com/webservices/";
publicstaticfinalStringURL="http://1**.5*.1**.1**/test/WEB_SERVIVE.asmx";
privateSoapObjectresponse=null;
publicWebRequestManager(Context context) {
}
publicvoidsendRequest(SoapObject requestObj, String methodName) {
try {
SoapSerializationEnvelopemySoapEnvelop=newSoapSerializationEnvelope(
SoapEnvelope.VER11);
// request.addProperty("sError", "");AndroidHttpTransporttransport=newAndroidHttpTransport(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() {
returnthis.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.*.*;
publicclassCustomer {
String customerTypeId;
String customerType;
String sDARCustomer;
// BEAN CLASS FOR Web servicepublicStringgetCustomerTypeId() {
return customerTypeId;
}
publicvoidsetCustomerTypeId(String customerTypeId) {
this.customerTypeId = customerTypeId;
}
publicStringgetCustomerType() {
return customerType;
}
publicvoidsetCustomerType(String customerType) {
this.customerType = customerType;
}
publicStringgetsDARCustomer() {
return sDARCustomer;
}
publicvoidsetsDARCustomer(String sDARCustomer) {
this.sDARCustomer = sDARCustomer;
}
}
3. CREATE AN INDEPENDENT PARSER CLASS
publicstaticArrayList<BEANCLASS> parseGetCustomerTypeResponse(
SoapObject response) {
ArrayList<BEANCLASS> customerList = new ArrayList<BEANCLASS>();
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 **
classGetCustomerTypeextendsAsyncTask<String, Void, Integer> {
private ProgressDialog progress;
@OverrideprotectedvoidonPreExecute() {
progress = ProgressDialog.show(GetCustomerTypeActivity.this, "",
"Loading ...");
}
@Overrideprotected Integer doInBackground(String... params) {
try {
WebRequestManagerrequestManager=newWebRequestManager(
GetCustomerTypeActivity.this);
SoapObjectrequest=newSoapObject(
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();
}
}
protectedvoidonPostExecute(Integer result) {
progress.dismiss();
//SET YOUR OWN ADAPTER HERE FOR SPINNER
adpater = newCustomerArrayAdapter(
GetCustomerTypeActivity.this,
R.layout.get_customer_type_imagerow,custArrList);
custTypeList.setAdapter(adpater);
progress.dismiss();
}
}
IN THE ACTIVITY PUT THE ADAPTER CODE LIKE BELOW :
classCustomerArrayAdapterextendsBaseAdapter {
private Context context;
privateint resID;
private ArrayList<BEAN CLASS> items;
publicCustomerArrayAdapter(Context context, int resID,
ArrayList<BEAN CLASS> items) {
this.context = context;
this.resID = resID;
this.items = items;
}
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn items.size();
}
@Overridepublic Object getItem(int arg0) {
// TODO Auto-generated method stubreturnnull;
}
@OverridepubliclonggetItemId(int arg0) {
// TODO Auto-generated method stubreturn0;
}
@Overridepublic View getView(int position, View row, ViewGroup parent) {
if (row == null) {
LayoutInflaterinflater= getLayoutInflater();
row = inflater.inflate(resID, null);
}
BEAN CLASSitem= items.get(position);
// THIS IS FOR CUSTOM LISTVIEW. CHANGE NAMES AS NECESSARYTextViewcustType= (TextView) row
.findViewById(R.id.textViewGetCust);
custType.setText(item.getCustomerType());
return row;
// FOR SPINNER CHANGE ABOVE CODE AS BELOW
BEAN CLASSitem= items.get(position);
TextViewtext= (TextView) row.findViewById(android.R.id.text1);
text.setText(item.getCityName());
text.setTextSize(20);
return row;
}
}
Post a Comment for "Get Array Item From Webservice Object To Android"