在android

时间:2015-11-28 12:29:59

标签: java c# android sql web-services

我有以下网络服务:

public class Student
{
    public int id;
    public string name;
    public string grade;
}

[WebMethod]
public Student StudentDetails(string sName)
{
    Student objStd = new Student();  

    SqlConnection conn;

    conn = Class1.ConnectionManager.GetConnection();

    conn.Open();

    SqlCommand newCmd = conn.CreateCommand();
    newCmd.CommandType = CommandType.Text;
    newCmd.CommandText = "select * from dbo.tblUser where name='" + sName + "'";
    SqlDataReader sdr = newCmd.ExecuteReader();

    if (sdr.Read())
    {
        objStd.id = Int32.Parse(sdr["Id"].ToString());
        objStd.name = sdr["name"].ToString();
        objStd.grade = sdr["grade"].ToString();
    }

    conn.Close();
    sdr.Close();
    return objStd;
}

返回xml:

以下
<?xml version="1.0" encoding="UTF-8"?>
<Student xmlns="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <id>4</id>
    <name>lama</name>
    <grade>7</grade>

</Student>

我想在我的Android应用程序中使用这些值,并在TextView中显示每个值。怎么做?

我使用此代码在TextView中显示单个值:

public class MainActivity extends AppCompatActivity {

    private EditText editText;

    private TextView textView;

    private Handler mHandler= new Handler();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        editText = (EditText)findViewById(R.id.editText);

        textView = (TextView)findViewById(R.id.textView);

    }

    public void getName(View v){

        String inputId =editText.getText().toString();

        //String[] params= new String[]{"10.0.2.2",inputId};

        String[] params= new String[]{"192.168.1.17:90",inputId};

        new MyAsyncTask().execute(params);

    }

class MyAsyncTask extends AsyncTask<String, Void, String> {

    public String SOAP_ACTION="http://tempuri.org/findUserNameById";

    public String OPERATION_NAME ="findUserNameById";

    public String WSDL_TARGET_NAMESPACE ="http://tempuri.org/";

    public String SOAP_ADDRESS;

    private SoapObject request;

    private HttpTransportSE httpTransport;

    private SoapSerializationEnvelope envelop;

    Object response= null;

    @Override

    protected String doInBackground(String... params) {

        SOAP_ADDRESS="http://"+params[0]+"/myWebService.asmx";

        request= new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

        PropertyInfo pi=new PropertyInfo();

        pi.setName("Id");

        pi.setValue(Integer.parseInt(params[1]));

        pi.setType(Integer.class);

        request.addProperty(pi);

        pi= new PropertyInfo();

        envelop= new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelop.dotNet=true;

        envelop.setOutputSoapObject(request);

        httpTransport=new HttpTransportSE(SOAP_ADDRESS);

        try{

            httpTransport.call(SOAP_ACTION,envelop);

            response=envelop.getResponse();

        }
        catch (Exception e){

            response=e.getMessage();

        }

        return response.toString();

    }

    @Override

    protected void onPostExecute(final String result){

        super.onPostExecute(result);

        mHandler.post(new Runnable() {

            @Override

            public void run() {

                textView.setText(result);

            }

        });

    }

}

但我如何单独使用每个值并在不同的文本视图中显示它们?

错误:

  import android.util.Log;

 import org.ksoap2.SoapEnvelope;
 import org.ksoap2.serialization.SoapSerializationEnvelope;
 import org.ksoap2.transport.Transport;


public class SoapAPIService {
private static final String XML_TAG_VERSION = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

public static GetStudentDetailsResult getStudentDetails(StudentRequest params) {
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = IsDotNet;
    envelope.setOutputSoapObject(params.getSoapParams());

    new GetStudentDetailsResult().register(envelope);

    Transport transport = getTransport();
    transport.setXmlVersionTag(XML_TAG_VERSION);
    try {
        transport.call(params.getSoapAction(), envelope);
        GetStudentDetailsResult result = (GetStudentDetailsResult) envelope.bodyIn;
        return result;
    }  catch (Exception e) {
        Log.e(TAG, "Exception", e);
    }
    return null;
}
public GetStudentDetailsResult getStudentDetails(String id) {
    StudentRequest request = new StudentRequest(id);
    GetStudentDetailsResult result = SoapAPIService.getStudentDetails(request);
    return result;
}
}

我收到了这些错误:

isDotNet ..无法解析符号。

getTransport ..无法解析方法。

TAG无法解析符号。

1 个答案:

答案 0 :(得分:1)

虽然您没有提及,但我相信您必须使用KSoap2库进行SOAP Web服务调用。考虑到这一点,您需要创建一些存根,如下所示:

1)创建一个BaseObject

import java.io.Serializable;

import org.ksoap2.serialization.KvmSerializable;

public abstract class BaseObject implements KvmSerializable, Serializable {

    private static final long serialVersionUID = 1L;

    public static final String NAMESPACE = "YOUR_WEB_SERVICE_NAMESPACE";

    public BaseObject() {
        super();
    }   
}

2)与您的请求结构保持一致的请求对象。到目前为止,我只能看到您的请求只需Id

public class StudentRequest {

    public static final String NAMESPACE = "YOUR_WEB_SERVICE_NAMESPACE";
    private static final String METHOD_NAME = "API_METHOD_NAME";

    private String id;

    // constructor
    public StudentRequest(String id) {
        this.id = id;
    }

    public SoapObject getSoapParams() {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("Id", id);

        return request;
    }

    public String getSoapAction() {
        return NAMESPACE + METHOD_NAME;
    }
}

3)响应对象

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapSerializationEnvelope;

import com.tc2services.stubs.BaseObject;

public class GetStudentDetailsResult extends BaseObject {
    public String id;
    public String name;
    private String grade;

    @Override
    public Object getProperty(int index) {
        switch (index) {
        case 0:
            return id;
        case 1:
            return name;
        case 2:
            return grade;
        }
        return null;
    }

    @Override
    public int getPropertyCount() {
        return 3;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch (index) {
        case 0:
            info.name = "id";
            info.type = String.class;
            break;
        case 1:
            info.name = "name";
            info.type = String.class;
            break;
        case 2:
            info.name = "grade";
            info.type = String.class;
            break;
        }

    }

    @Override
    public void setProperty(int index, Object obj) {
        switch (index) {
        case 0:
            id = (String) obj;
            break;
        case 1:
            name = (String) obj;
            break;
        case 2:
            grade = (String) obj;
            break;
        }

    }

    public void register(SoapSerializationEnvelope envelope) {
        envelope.addMapping(NAMESPACE, "YOUR_RESPONSE_METHOD_NAME", this.getClass());
    }
}

4)要调用,您可以拥有一个单独的服务类,比如SoapAPIService暴露apis,如下所示:

import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.Transport;

public boolean IsDotNet = true;

public Transport getTransport() {
    return new HttpTransportSE(WEB_SERVICE_URL);
}

private static final String XML_TAG_VERSION = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

public static GetStudentDetailsResult getStudentDetails(StudentRequest params) {
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = IsDotNet;
    envelope.setOutputSoapObject(params.getSoapParams());

    new GetStudentDetailsResult().register(envelope);

    Transport transport = getTransport();
    transport.setXmlVersionTag(XML_TAG_VERSION);
    try {
        transport.call(params.getSoapAction(), envelope);
        GetStudentDetailsResult result = (GetStudentDetailsResult) envelope.bodyIn;
        return result;
    }  catch (Exception e) {
        Log.e(TAG, "Exception", e);
    }
    return null;
}

5)最后,对于调用:

public GetStudentDetailsResult getStudentDetails(String id) {
    StudentRequest request = new StudentRequest(id);
    GetStudentDetailsResult result = SoapAPIService.getStudentDetails(request);
    return result;
}

请注意,您可能需要对方法名称,URL,属性等进行一些调整,但下面是您希望使用存根方法使用kso​​ap2进行Web服务调用时的基本框架。

希望它有所帮助!