问题
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.
回答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;
}
}
来源:https://stackoverflow.com/questions/19650503/get-array-item-from-webservice-object-to-android