java - 使用 C# Web 服务连接 Android 设备时出错

标签 java c# android soap ksoap2

我正在尝试使用简单的 C# Web 服务连接我的 Android 设备,但收到此错误

org.xmlpull.v1.XmlpullparserException:expected:START_TAG{http://schemas.xmlsoap.org/soap/envelope/} Envelope(position:START_TAG@2:7 in java.io.inputStreamReader@1db98270)

这是我的代码:

package com.example.khalifa_.webservice;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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://192.168.1.3:29824/Service.asmx";
// for the SOAP_ADDRESS, run your web service & see
//your web service Url :1506/WebSite3/Service.asmx ,1052 will be change according to your PC

    TextView tvData1;
    EditText edata;
    Button button;
    String studentNo;
//http://localhost:1827/WebSite1/Service.asmx/HelloWorld

    //http://10.0.2.2:1827/WebSite1/Service.asmx
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        tvData1 = (TextView)findViewById(R.id.textView1);

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

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,     OPERATION_NAME);
                PropertyInfo propertyInfo = new PropertyInfo();
                propertyInfo.type = PropertyInfo.STRING_CLASS;
                propertyInfo.name = "eid";

                edata =(EditText)findViewById(R.id.editText1);
                studentNo=edata.getText().toString();

                request.addProperty(propertyInfo, studentNo);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.dotNet = true;

                envelope.setOutputSoapObject(request);

                HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

                try  {
                    httpTransport.call(SOAP_ACTION, envelope);
                    Object response = envelope.getResponse();
                    tvData1.setText(response.toString());
                }  catch (Exception exception)   {
                    tvData1.setText(exception.toString()+"  Or enter number     is not Available!");
                }

                tvData1 = (TextView)findViewById(R.id.textView1);
            }
        });

        //client = new DefaultHttpClient();
        //new Read().execute("text");
    }
}

最佳答案

您必须在后台线程中编写代码。使用 asynctask 作为后台线程。了解更多refer this

关于java - 使用 C# Web 服务连接 Android 设备时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30312728/

相关文章:

java - Azure 移动应用程序自定义 api 异常

android - 如何从 Developer Console 读取崩溃报告

java - 如何在java中对列表对象(int)进行排序?

java - 使用从 Json 响应传递的对象填充 JTable

java - Long 的大小为 8 字节,那么在 JAVA 中如何将 'promoted' 转换为 float (4 字节)?

c# - .NET 将十六进制值的字符串转换为 Unicode 字符(支持不同的代码页)

c# - Orchard 项目模块出现错误 : No persister for: SomePartRecord

c# - C#中使用XML文件存储数据

java - JPA实体可以序列化到磁盘吗?

Android不匹配标签异常突然