无法连接到我的Web服务

时间:2012-07-19 11:15:28

标签: android web-services

我从Android应用程序连接我的Web服务时遇到问题。也许我使用了错误的参数?

的活动:

public class MainActivity extends Activity {

final private String METHOD_NAME = "getMenuePlan";
final private String NAMESPACE = "http://webservice.kantine.de/";
final private String SOAP_ACTION = "\"" + NAMESPACE + METHOD_NAME + "\"";
final private String URL = "http://10.0.2.2:8080/Kantine/MenuePlan?wsdl";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(URL);
    SoapObject result = null;


    try {

        httpTransport.call(SOAP_ACTION, envelope);
        result = (SoapObject) envelope.getResponse();

    } catch (SoapFault e) {

        Log.e("error", "", e);

    } catch (IOException e) {

        Log.e("error", "", e);

    } catch (XmlPullParserException e) {

        Log.e("error", "", e);

    }

    if (result != null) {
        Log.i("Result", result.toString());
    } else {
        Log.i("Result", "null");
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

WSDL:

<wsdl:definitions name="KantineWSImplService" targetNamespace="http://webservice.kantine.de/"><wsdl:types><xs:schema elementFormDefault="unqualified" targetNamespace="http://webservice.kantine.de/" version="1.0"><xs:element name="getMenuePlan" type="tns:getMenuePlan"/><xs:element name="getMenuePlanResponse" type="tns:getMenuePlanResponse"/><xs:complexType name="getMenuePlan"> [...]

例外:

    7-19 10:58:10.287: E/AndroidRuntime(1008): FATAL EXCEPTION: main
07-19 10:58:10.287: E/AndroidRuntime(1008): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wstest/com.example.wstest.MainActivity}: android.os.NetworkOnMainThreadException
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.os.Looper.loop(Looper.java:137)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at java.lang.reflect.Method.invoke(Method.java:511)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at dalvik.system.NativeStart.main(Native Method)
07-19 10:58:10.287: E/AndroidRuntime(1008): Caused by: android.os.NetworkOnMainThreadException
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.io.IoBridge.connect(IoBridge.java:112)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at java.net.Socket.connect(Socket.java:842)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:109)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:157)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at com.example.wstest.MainActivity.onCreate(MainActivity.java:41)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.app.Activity.performCreate(Activity.java:5008)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-19 10:58:10.287: E/AndroidRuntime(1008):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-19 10:58:10.287: E/AndroidRuntime(1008):     ... 11 more

我已添加到清单中。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

OMG ......为什么每个打电话给WebService

的开玩家都会犯同样的错误

这是answer.

网络请求只能在ANDROID的背景线上进行&gt; = 3.0

答案 1 :(得分:0)

您应该在单独的线程中进行网络呼叫.. How to fix android.os.NetworkOnMainThreadException?