简体   繁体   English

如何从android中的SOAP响应获取数据?

[英]How to get data from SOAP response in android?

What I am trying to do is create a simple Web Service in ASP.NET and get Data from MSSQL server through that web service and use that data in my android application. 我想做的是在ASP.NET中创建一个简单的Web服务,并通过该Web服务从MSSQL服务器获取数据,然后在我的android应用程序中使用该数据。 As a novice, I have tried out this Helpful link to follow. 作为新手,我尝试了以下有用的链接。 I have used the same architecture as this tutorial suggest. 我使用了本教程建议的相同体系结构。 But getting error in parsing the result (Soap Response) in my result. 但是在我的结果中解析结果(Soap Response)时出错。 It is as below. 如下。

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> <soap:Body><findContactResponse xmlns="http://tempuri.org/" />
> </soap:Body> </soap:Envelope>

and the GetPropertyCount() method is returning 0, on the other hand, arrayindexoutofBound error is coming up with index 0. 并且GetPropertyCount()方法返回0,另一方面,出现index 0的arrayindexoutofBound错误。

Here is my activity code. 这是我的活动代码。 Where I have implemented async task for easy handling. 我在哪里实现了异步任务以便于处理。

public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://tempuri.org/findContact";

    private static final String OPERATION_NAME = "findContact";// your webservice web method name

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

    private static final String SOAP_ADDRESS = "http://10.0.2.2:58497/WebService/Service.asmx?wsdl";//"http://127.0.0.1:58497/WebService/Service.asmx";

    protected static final String TAG = null;

    TextView tvData1;
    EditText edata;
    Button button;
    String studentNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvData1 = (TextView)findViewById(R.id.textView1);
        edata =(EditText)findViewById(R.id.editText1);

        button=(Button)findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                    studentNo=edata.getText().toString();
                    new Submit().execute(studentNo);
            }
        });
    }

    private class Submit extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... arg) {

            // TODO Auto-generated method stub
            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
            PropertyInfo propertyInfo = new PropertyInfo();
            propertyInfo.type = PropertyInfo.STRING_CLASS;
            propertyInfo.name = "eid";

            Log.e("studentNo", studentNo);
            request.addProperty(propertyInfo);
            request.addProperty("eid", studentNo);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
            httpTransport.debug = true;
            try  { 

                    httpTransport.call(SOAP_ACTION, envelope);
                    Log.e("Dump", httpTransport.responseDump.toString());
                SoapObject result=(SoapObject)envelope.bodyIn;
                if(result!= null){

                    Log.e("response", result.toString());
                    //To get the data.
                    System.out.println("********Count : "+ result.getPropertyCount());
                    String resultData=result.getProperty(0).toString();
                    Log.e("Found", resultData);
                }
                else{

                    Log.e("Obj", result.toString());
                }
            }  
            catch (Exception exception)   {

                Log.e("Not Found", exception.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
}

Now can anyone just show me what went wrong for me? 现在谁能告诉我我怎么了? Or how can I get my response without exception? 或者我如何能毫无例外地得到我的回应? After debugging I came up with this line which is creating exceptions. 调试后,我想到了创建异常的这一行。

String resultData=result.getProperty(0).toString();

Thanks is advance for any kind of help. 感谢您提供任何帮助。

Finally figured it out. 终于想通了。

It was in adding property. 这是在增加财产。 And slightly changing the response parsing. 并稍微更改响应解析。

Here is the complete code which worked out for me. 这是为我工作的完整代码。

Hope it helps others. 希望它能帮助别人。

private static final String SOAP_ACTION = "http://tempuri.org/findContact";

    private static final String OPERATION_NAME = "findContact";// your webservice web method name

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

    private static final String SOAP_ADDRESS = "http://10.0.2.2:58497/WebService/Service.asmx";

    protected static final String TAG = null;
    private static String fahren;

    TextView tvData1;
    EditText edata;
    Button button;
    String studentNo;
    String state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvData1 = (TextView)findViewById(R.id.textView1);
        edata =(EditText)findViewById(R.id.editText1);

        button=(Button)findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                studentNo=edata.getText().toString();
                new Submit().execute(studentNo);
            }
        });
    }

    private class Submit extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected String doInBackground(String... arg) {
            // TODO Auto-generated method stub
            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
            PropertyInfo propertyInfo = new PropertyInfo();
            propertyInfo.type = PropertyInfo.STRING_CLASS;
            propertyInfo.name = "eid";
            propertyInfo.setValue(studentNo);
            request.addProperty(propertyInfo);//problem was here

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
            httpTransport.debug = true;
            try{ 
                httpTransport.call(SOAP_ACTION, envelope);

                Log.e("RequestDump", httpTransport.requestDump.toString());
                Log.e("ResponseDump", httpTransport.responseDump.toString());

                SoapObject result=(SoapObject)envelope.bodyIn;
                if(result!= null){
                    state = result.getProperty(0).toString();
                    Log.e("Found", state);
                }
                else{
                    Log.e("Obj", result.toString());
                }
            }  
            catch (Exception exception)   {
                Log.e("Exception", exception.toString());
            }
            return state;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            tvData1.setText(result);
        }
    }
}

I have implemented an async task as well to get rid of run time exceptions on Main thread. 我还实现了一个异步任务,以摆脱主线程上的运行时异常。 Need to add the request.addProperty(propertyInfo) properly. 需要正确添加request.addProperty(propertyInfo)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM