1

I have below webservice:

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;
}

which returns below 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>

I want to use these values in my android application and display each value in a TextView. how to do this?

I used this code to display single value in a 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);

            }

        });

    }

}

but how can i use each value individually and display them in different textviews?

ERROR:

  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;
}
}

i got these errors:

isDotNet .. cannot resolve symbol.

getTransport .. cannot resolve method.

TAG cannot resolve symbol.

1 Answer 1

1

Although you haven't mentioned, I believe you must be using KSoap2 library for SOAP web service call. Considering that, you need to create a few stubs as follows:

1) Create a BaseObject class

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) A request object that aligns with your request structure. As far I could see your request needed only 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) A response object

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) To invoke you can have a separate service class, say, SoapAPIService exposing apis such as below:

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) Finally, for invocation:

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

Please, note, you might need to make a few tweaks with method names, URLs, properties, etc, but below is the basic framework when you want to follow stub approach to make web service calls with ksoap2.

Hope it helps!

Sign up to request clarification or add additional context in comments.

4 Comments

StudentRequest, GetStudentDetailsResult both are new classes i create?
also step 4 and 5 it should be in the mainactivity class or where?
Yes for better code modularity, you can put them in separate classes. For 4 and 5, as I mentioned you could create a new class say SoapAPIService and all you web service api methods here, including getStudentDetails
can you check the last part of my post i updated it, i got errors in the SoapAPIService class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.