4

我无法从 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

我已添加到清单中。

有什么建议么?

4

2 回答 2

1

OMG ...为什么每个调用WebService

这是答案。.

网络请求只能在 ANDROID >= 3.0 的后台线程上进行

于 2012-07-19T11:20:00.620 回答
0

您应该在单独的线程中进行网络调用。 如何修复 android.os.NetworkOnMainThreadException?

于 2012-07-19T11:17:02.547 回答