未连接到Android KSOAP Webservice

时间:2014-05-15 05:01:57

标签: android web-services ksoap2

我的localhost上有一个web服务,它有一个连接数据库的简单方法,并且还返回字符串数组中的城市名称。

以下是我运行服务时的链接:

http://localhost:1543/Service1.asmx

我尝试通过Android应用程序连接到此网络服务,但我认为我没有正确选择namespacesoapaction等等,所以我收到以下内容异常。

预期:START_TAG {http://schemas.xmlsoap.org/soap/envelope/}信封(位置:START_TAG @ 2:7 in java.io.InputStreamReader@41387778)

这是我的Android代码:

    public class ProvinceCityActivity extends Activity
{
    EditText txtProvince;
    Button btnResult;

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

    private static String NAMESPACE = "http://tempuri.org/";
    private static String METHOD_NAME = "GetCities";

    private static String URL = "http://192.168.0.109:1543/Service1.asmx";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtProvince = (EditText) findViewById(R.id.txtProvince);
        btnResult = (Button) findViewById(R.id.btnResult);
        btnResult.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("value", txtProvince.getText().toString());
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                envelope.dotNet = true;


                try
                {
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);
                    androidHttpTransport.call(SOAP_ACTION, envelope);


                    SoapObject result = (SoapObject) envelope.bodyIn;
                    if (result != null)
                    {
                        String[] strResult = (String[]) result.getProperty(0);
                    Toast.makeText(getApplicationContext(),
                            "Cities: " + strResult[0], 6000).show();
                    }
                } catch (Exception e)
                {
                    Toast.makeText(getApplicationContext(),
                            e.getMessage().toString(), 6000).show();
                }

            }
        });

    }
}

logcat的:

05-15 05:10:07.651: D/dalvikvm(632): GC_CONCURRENT freed 101K, 78% free 462K/2048K, paused 1ms+1ms
05-15 05:10:07.661: D/dalvikvm(632): Debugger has detached; object registry had 1 entries
05-15 05:10:07.671: I/AndroidRuntime(632): NOTE: attach of thread 'Binder Thread #3' failed
05-15 05:10:08.252: D/AndroidRuntime(645): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
05-15 05:10:08.252: D/AndroidRuntime(645): CheckJNI is ON
05-15 05:10:09.132: D/AndroidRuntime(645): Calling main entry com.android.commands.am.Am
05-15 05:10:09.161: I/ActivityManager(73): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=soleimannezhad.android.wCFProvinceCity/.Sohsession13WCFProvinceCityActivity} from pid 645
05-15 05:10:09.171: W/WindowManager(73): Failure taking screenshot for (180x300) to layer 21005
05-15 05:10:09.211: W/NetworkManagementSocketTagger(73): setKernelCountSet(10047, 1) failed with errno -2
05-15 05:10:09.221: D/AndroidRuntime(645): Shutting down VM
05-15 05:10:09.241: D/dalvikvm(645): GC_CONCURRENT freed 102K, 77% free 483K/2048K, paused 2ms+1ms
05-15 05:10:09.241: D/dalvikvm(645): Debugger has detached; object registry had 1 entries
05-15 05:10:09.271: I/AndroidRuntime(645): NOTE: attach of thread 'Binder Thread #3' failed
05-15 05:10:09.751: V/PhoneStatusBar(141): setLightsOn(true)
05-15 05:10:10.191: V/PhoneStatusBar(141): setLightsOn(true)
05-15 05:10:10.321: I/ActivityManager(73): Displayed soleimannezhad.android.wCFProvinceCity/.Sohsession13WCFProvinceCityActivity: +1s115ms
05-15 05:10:10.631: D/dalvikvm(73): GC_CONCURRENT freed 489K, 6% free 12719K/13447K, paused 5ms+54ms
05-15 05:10:10.782: W/NetworkManagementSocketTagger(73): setKernelCountSet(10004, 0) failed with errno -2

5 个答案:

答案 0 :(得分:2)

见图像

enter image description here

SOAP_ACTION :这是您的NAMESPACE / METHOD_NAME,例如“http://ws.android4.com

METHOD_NAME :这是方法的名称,例如:“sayHello”

NAMESPACE :这是你的NameSapce,例如:“http://ws.android4.com

对于示例网络服务,请参阅此webservice of w3schools

下面,

METHOD_NAME:CelsiusToFahrenheit

SOAPAction:http://www.w3schools.com/webservices/CelsiusToFahrenheit

NAMESPACE:http://www.w3schools.com/webservices/

向Androidanifest.xml文件添加Internet权限

答案 1 :(得分:2)

您是否从Android模拟器调试您的应用程序?如果是这样,您应该使用“http:// 10.0.2.2”启动您的URL,以便模拟器可以连接到您的本地Web服务......

答案 2 :(得分:0)

package com.example.WSTest;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

//import android.widget.SlidingDrawer;;

public class WSTestActivity extends Activity {

    private static final String SOAP_ACTION = "http://MVIndia.com/AddActivity";
    private static final String METHOD_NAME = "AddActivity";
    private static final String NAMESPACE = "http://MVIndia.com/";
    private static final String URL = "http://10.0.2.2/SAP/Service.asmx";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //final SlidingDrawer txt1 = (SlidingDrawer) findViewById(R.id.Spinner1);
        //final SlidingDrawer txt2 = (SlidingDrawer) findViewById(R.id.Spinner2);
        final EditText txt3 = (EditText) findViewById(R.id.editText3);
        final EditText txt4 = (EditText) findViewById(R.id.editText4);
        final EditText txt5 = (EditText) findViewById(R.id.editText5);
        final EditText txt6 = (EditText) findViewById(R.id.editText6);
        final EditText txt7 = (EditText) findViewById(R.id.editText7);
        final EditText txt8 = (EditText) findViewById(R.id.editText8);
        //final SlidingDrawer txt9 = (SlidingDrawer) findViewById(R.id.Spinner3);
        final EditText txt10 = (EditText) findViewById(R.id.editText10);

        Button button = (Button) findViewById(R.id.btnActivity);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                SoapObject req_AddActivity = new SoapObject(NAMESPACE, METHOD_NAME);
                req_AddActivity.addProperty("strActivity", "");
                req_AddActivity.addProperty("strActivityType", "");
                req_AddActivity.addProperty("strAssignedTo", txt3.getText().toString());
                req_AddActivity.addProperty("strBPCode", txt4.getText().toString());
                req_AddActivity.addProperty("strBPName", txt5.getText().toString());
                req_AddActivity.addProperty("strActivityDate", txt6.getText().toString());
                req_AddActivity.addProperty("strContactPerson", txt7.getText().toString());
                req_AddActivity.addProperty("strEndDate", txt8.getText().toString());
                req_AddActivity.addProperty("strProgress", "");
                req_AddActivity.addProperty("strRemarks", txt10.getText().toString());

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true; 
                envelope.setOutputSoapObject(req_AddActivity);
                HttpTransportSE  androidHttpTransport = new HttpTransportSE (URL);      
                try {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    Object result = envelope.getResponse();
                    //if (result.toString()=="True")
                    //{
                        //Toast.makeText(WSTestActivity.this, "Activity added Successfully.", Toast.LENGTH_SHORT).show(); 
                    //}
                   Toast.makeText(WSTestActivity.this, result.toString(), Toast.LENGTH_SHORT).show();  
                    //((TextView)findViewById(R.id.lblStatus)).setText(result.toString());
                } catch(Exception E) {
                    Toast.makeText(WSTestActivity.this, E.getClass().getName() + ": " + E.getMessage(), Toast.LENGTH_SHORT).show(); 
                    //((TextView)findViewById(R.id.lblStatus)).setText("ERROR:" + E.getClass().getName() + ": " + E.getMessage());
                }
                //Toast.makeText(WSTestActivity.this, "Caught", Toast.LENGTH_SHORT).show();  
            }
        });
    }
}

试试这个在我的本地机器上工作。如果成功添加活动,Webservice将返回true。我希望它会对你有所帮助。

答案 3 :(得分:0)

我会把我的修补程序扔到那里......我已经多次遇到这种情况,而且过去每次都是行动或方法或网址。但是,这次它是一个奇怪的URL ...

我正在浏览当前项目的代理。出于某种原因,URL区分大小写。

https://abc.gov/webservice/webservice.asmx有效

https://abc.gov/WEBSERVICE/webservice.asmx无法正常工作

希望这会有所帮助。

答案 4 :(得分:-1)

如果您未提供正确的网址,则会出现此错误。请检查您托管Web服务的Web服务的URL,方法名称和Soap Action。使用像AsyncTask这样的后台服务来调用webservices是一种很好的做法。