Asp.如果Net Web Service有参数,则返回空响应

本文关键字:返回 响应 参数 如果 Net Web Service Asp | 更新日期: 2023-09-27 18:14:00

我正在尝试开发一个android应用程序,可以通过Web服务访问sql server数据库中存储的值。我的Web服务返回空响应(如anyType{}),如果它有参数。没有参数的Web方法工作得很好。这是我的Web服务

[WebMethod]
public List<string> getDetails(string dist)
{
    details = new List<string>();
    string sel = "select h_Name,h_address,h_contact from hospital_tb    where h_district='" + dist + "'";
    SqlDataReader dr = db.dataRead(sel);
    while (dr.Read())
    {
        string name = dr["h_name"].ToString();
        string adres = dr["h_address"].ToString();
        string contact = dr["h_contact"].ToString();
        result = name + ":" + adres + ":" + contact;
        details.Add(result);
    }
    return details;
} 

Web服务命名空间为:http://hospitalsearch.com/hos

这是我的Android代码

class Async extends AsyncTask<String, String, String>
{
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        request=new SoapObject(NAMESPACE, METHOD);
        property=new PropertyInfo();
        property.setName("district");
        property.setValue(dis);
        Log.d("condition", dis);
        request.addProperty(property);
        SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.bodyOut=request;
        Log.d("ree", request.toString());
        HttpTransportSE transport=new HttpTransportSE(ADDRESS);
        try {
            transport.call(ACTION, envelope);
            //SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
            response=(SoapObject)envelope.getResponse();
            Log.d("response", response.toString());
            if(response!=null)
            {
                parseData(response);
            }
            else
            {
                Log.d("result", "no data");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            res="error";
            Log.d("result", res);
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            res="error1";
            Log.d("result", res);
            e.printStackTrace();
        }
        return res;
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        ll.setAdapter(adapter);
    }
    public void parseData(SoapObject result)
    {
        for(int i=0;i<result.getPropertyCount();i++)
        {
            if(result.getProperty(i) instanceof SoapObject)
            {
                parseData((SoapObject)result.getProperty(i));
            }
            else
            {
                PropertyInfo p=new PropertyInfo();
                result.getPropertyInfo(i, p);
                String name= result.getProperty(i).toString();
                Log.d("data", name);
                String[] re=name.split(":");
                Model model=new Model();
                model.setName(re[0]);
                model.setAddress(re[1]);
                model.setContact(re[2]);
                al.add(model);
            }
        }
    }
}

在这里,我发现web服务没有接收到我从Android代码传递的参数。我也记录我的请求:日志。d("request", request. tostring());并显示getDetails{districts=AAAA}。

Asp.如果Net Web Service有参数,则返回空响应

1)创建worker,如果需要创建另一个action name,更改SoapRequest类。

public abstract class SoapWorker extends AsyncTask<SoapWorker.SoapRequest,Void,Object> {
public static class SoapRequest{
    private LinkedHashMap<String,Object> params;
    private String methodName;
    private String namespace;
    private String actionName;
    private String url;
    public SoapRequest(String url, String methodName,String namespace){
        this.methodName = methodName;
        this.params = new LinkedHashMap<>();
        this.namespace=namespace;
        this.actionName=this.namespace + "IService/" + methodName;
        this.url=url;
    }
    public void addParam(String key,Object value){
        this.params.put(key,value);
    }
}
@Override
protected Object doInBackground(SoapRequest...  inputs) {
          SoapRequest input=inputs [0];
    try {
        SoapObject request = new SoapObject(input.namespace, input.methodName);
        for(Map.Entry<String, Object> entry : input.params.entrySet()){
            request.addProperty(entry.getKey(),entry.getValue());
        }
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(input.url);
        androidHttpTransport.call(input.actionName, envelope);
        input.params.clear();
        return parseResponse(envelope.getResponse());
    } catch (Exception e) {
        Log.e("SoapWorker", "error " + e);
        return e;
    }
}
@WorkerThread
public abstract Object parseResponse(Object response);

}

2)发出请求:

SoapWorker.SoapRequest request = new SoapWorker.SoapRequest(URL,METHOD,NAMESPACE);
    request.addParam(param1,value1);
    ...
    request.addParam(paramN, valueN);
    //for example:
    //request.addParam("district", dis);

    SoapWorker worker = new SoapWorker(){
        @Override
        public Object parseResponse(Object response) {
            //parse and return results
            //Log your response to see what you get from server
            return result;
        }
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            //update your model on UI
        }
    };
    worker.execute(request);