我试图获得类似的细节。区域,名称,身份...从MsSql DB到数据对象的.Net Web服务。
我的网络服务
[的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;
}
我的KvmSerializable类
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;
}
}
我的活动类
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);
}
}
我需要把所有的区域,名称,id都放到微调器中。 我可以将它绑定到微调器但是如何将数组obj的详细信息作为来自Web服务的字符串。请帮助。
答案 0 :(得分:1)
好吧这很冗长。一步一步地采取行动
<强> 1。为Web服务请求创建一个新类:
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.根据您的网站服务响应创建一个豆类**
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。创建一个独立的PARSER类
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.遵循您的活动代码。请记住根据您的适配器和方法的名称更改内容**
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();
}
}
在活动中将适配器代码放在下面:
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;
}
}