ksoap2出错

时间:2014-05-28 08:37:40

标签: android ksoap2

我使用WCF来托管.svc服务以从MySQL(在本地主机上)检索数据,我已经编写了一个类来使用Android上的服务,使用kso​​ap2版本3.1.1和IP 10.0.3.2进行Geny Motion访问本地主机

package com.example.mapdemo;

import java.io.IOException;
import java.net.Proxy;
import java.net.SocketTimeoutException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class KetNoiWebService {
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String MAIN_REQUEST_URL = "http://10.0.3.2:7741/ToaDoServiceHost.svc";
    private static final String SOAP_ACTION = "http://tempuri.org/IToaDo/GetAllToaDo";

    private static final SoapSerializationEnvelope getSoapSerializationEnvelope(
            SoapObject request) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);

        return envelope;
    }

    private static final HttpTransportSE getHttpTransportSE() {
        HttpTransportSE ht = new HttpTransportSE(MAIN_REQUEST_URL, 60000);

        return ht;
    }

    public static int getAllToaDoTuWebService() {
        int count = 1;

        String methodname = "GetAllToaDo";

        SoapObject request = new SoapObject(NAMESPACE, methodname);

        SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);

        HttpTransportSE ht = getHttpTransportSE();

        try {
            ht.call(SOAP_ACTION, envelope);

            SoapObject resultsString = (SoapObject) envelope
                    .bodyIn;
            SoapObject result = (SoapObject)resultsString.getProperty(0);

            count = result.getPropertyCount();

        } catch (SocketTimeoutException t) {
            t.printStackTrace();
        } catch (IOException i) {
            i.printStackTrace();
        } catch (Exception q) {
            q.printStackTrace();
        }
        return count;
    }
}

并在活动

上调用该类
package com.example.mapdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class KhuSanXuatActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_khusanxuat);

        ((TextView)findViewById(R.id.webservicecount))
        .setText((KetNoiWebService.getAllToaDoTuWebService()));
    }


}

activity_khusanxuat.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="San pham dang co trong kho:" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text=" - Bap u chua: " />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="100 "
            android:textColor="@android:color/holo_blue_light"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cuon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text=" - Phan ga: " />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="50 "
            android:textColor="@android:color/holo_blue_light"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tan" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Du toan san luong:" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text=" - Bap u chua" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="    + Ngay thu hoach du kien: " />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/holo_blue_light"
            android:text="01/07/2014"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="    + San luong du kien: " />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="200 "
            android:textColor="@android:color/holo_blue_light"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cuon" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text=" - Phan ga" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="    + Ngay thu hoach du kien: " />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="20/07/2014"
            android:textColor="@android:color/holo_blue_light"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="    + San luong du kien: " />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="150 "
            android:textColor="@android:color/holo_blue_light"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="tan" />
    </LinearLayout>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webservicecount"/>
</LinearLayout>

并且出现错误

05-28 14:23:37.082: W/System.err(2574): android.os.NetworkOnMainThreadException
05-28 14:23:37.086: W/System.err(2574):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-28 14:23:37.086: W/System.err(2574):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-28 14:23:37.090: W/System.err(2574):     at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
05-28 14:23:37.090: W/System.err(2574):     at libcore.io.IoBridge.connect(IoBridge.java:112)
05-28 14:23:37.094: W/System.err(2574):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-28 14:23:37.098: W/System.err(2574):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-28 14:23:37.098: W/System.err(2574):     at java.net.Socket.connect(Socket.java:842)
05-28 14:23:37.102: W/System.err(2574):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
05-28 14:23:37.106: W/System.err(2574):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-28 14:23:37.106: W/System.err(2574):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
05-28 14:23:37.106: W/System.err(2574):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-28 14:23:37.110: W/System.err(2574):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-28 14:23:37.110: W/System.err(2574):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
05-28 14:23:37.110: W/System.err(2574):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
05-28 14:23:37.110: W/System.err(2574):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
05-28 14:23:37.110: W/System.err(2574):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
05-28 14:23:37.114: W/System.err(2574):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
05-28 14:23:37.114: W/System.err(2574):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
05-28 14:23:37.114: W/System.err(2574):     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
05-28 14:23:37.118: W/System.err(2574):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:185)
05-28 14:23:37.118: W/System.err(2574):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
05-28 14:23:37.118: W/System.err(2574):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
05-28 14:23:37.118: W/System.err(2574):     at com.example.mapdemo.KetNoiWebService.getAllToaDoTuWebService(KetNoiWebService.java:47)
05-28 14:23:37.122: W/System.err(2574):     at com.example.mapdemo.KhuSanXuatActivity.onCreate(KhuSanXuatActivity.java:16)
05-28 14:23:37.126: W/System.err(2574):     at android.app.Activity.performCreate(Activity.java:5008)
05-28 14:23:37.126: W/System.err(2574):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-28 14:23:37.126: W/System.err(2574):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-28 14:23:37.126: W/System.err(2574):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-28 14:23:37.130: W/System.err(2574):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-28 14:23:37.130: W/System.err(2574):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-28 14:23:37.130: W/System.err(2574):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 14:23:37.130: W/System.err(2574):     at android.os.Looper.loop(Looper.java:137)
05-28 14:23:37.130: W/System.err(2574):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-28 14:23:37.130: W/System.err(2574):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 14:23:37.130: W/System.err(2574):     at java.lang.reflect.Method.invoke(Method.java:511)
05-28 14:23:37.130: W/System.err(2574):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-28 14:23:37.130: W/System.err(2574):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-28 14:23:37.130: W/System.err(2574):     at dalvik.system.NativeStart.main(Native Method)
05-28 14:23:37.130: W/ResourceType(2574): No package identifier when getting value for resource number 0x00000001
05-28 14:23:37.134: D/AndroidRuntime(2574): Shutting down VM
05-28 14:23:37.134: W/dalvikvm(2574): threadid=1: thread exiting with uncaught exception (group=0xa6284288)
05-28 14:23:37.222: E/AndroidRuntime(2574): FATAL EXCEPTION: main
05-28 14:23:37.222: E/AndroidRuntime(2574): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.KhuSanXuatActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.os.Looper.loop(Looper.java:137)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at java.lang.reflect.Method.invoke(Method.java:511)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at dalvik.system.NativeStart.main(Native Method)
05-28 14:23:37.222: E/AndroidRuntime(2574): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.content.res.Resources.getText(Resources.java:229)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.widget.TextView.setText(TextView.java:3620)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at com.example.mapdemo.KhuSanXuatActivity.onCreate(KhuSanXuatActivity.java:16)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.app.Activity.performCreate(Activity.java:5008)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-28 14:23:37.222: E/AndroidRuntime(2574):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-28 14:23:37.222: E/AndroidRuntime(2574):     ... 11 more
05-28 14:23:41.590: I/Process(2574): Sending signal. PID: 2574 SIG: 9

我已经在Geny Motion浏览器上测试了URL,我还编写了一个Java控制台应用程序并使用上面的类成功使用了该服务,任何人都可以帮助我,非常感谢!

http://localhost:7741/ToaDoServiceHost.svc?wsdl

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" name="ToaDoService" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://sang-pc:7741/ToaDoServiceHost.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://sang-pc:7741/ToaDoServiceHost.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="http://sang-pc:7741/ToaDoServiceHost.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/BogacaServices"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IToaDo_GetAllToaDo_InputMessage">
<wsdl:part name="parameters" element="tns:GetAllToaDo"/>
</wsdl:message>
<wsdl:message name="IToaDo_GetAllToaDo_OutputMessage">
<wsdl:part name="parameters" element="tns:GetAllToaDoResponse"/>
</wsdl:message>
<wsdl:portType name="IToaDo">
<wsdl:operation name="GetAllToaDo">
<wsdl:input wsaw:Action="http://tempuri.org/IToaDo/GetAllToaDo" message="tns:IToaDo_GetAllToaDo_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IToaDo/GetAllToaDoResponse" message="tns:IToaDo_GetAllToaDo_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IToaDo" type="tns:IToaDo">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetAllToaDo">
<soap:operation soapAction="http://tempuri.org/IToaDo/GetAllToaDo" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ToaDoService">
<wsdl:port name="BasicHttpBinding_IToaDo" binding="tns:BasicHttpBinding_IToaDo">
<soap:address location="http://sang-pc:7741/ToaDoServiceHost.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

3 个答案:

答案 0 :(得分:0)

您需要使用AsyncTask来执行所有网络操作。

您的网络操作可能会花费大量时间,如果在主UI线程上完成,UI将无法响应。如果您的用户界面长时间冻结,应用程序可能会被操作系统杀死。

因此,Android 4+强制要求使用后台线程来执行网络操作。

使用 doInBackground() 将代码放在 execute() 和所有AsyncTask中进行网络活动。

以下是您的AsyncTask的样子:

private class SendMail extends AsyncTask<String, Integer, Void> {
     protected void doInBackground() {
        sendEmail();
 }

 protected void onProgressUpdate() {
    //called when the background task makes any progress
 }

  protected void onPreExecute() {
     //called before doInBackground() is started
 }
 protected void onPostExecute() {
     //called after doInBackground() has finished 
 }
  }

您可以使用 new SendMail().execute("");

在任何地方拨打电话

答案 1 :(得分:0)

您不应该在主线程(UI线程)中调用Web服务。 您应该使用Services或AsyncTask来执行相同的操作

答案 2 :(得分:0)

class DoInBack extends AsyncTask<String, Void, String> {

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

        }

        @Override
        protected String doInBackground(String... urls) {
            String res = "";
            try {

                res=KetNoiWebService.getAllToaDoTuWebService();

            } catch (Exception e) {
                e.printStackTrace();
            }
            return res;
        }

        @Override
        protected void onPostExecute(String result) {

            Toast.makeText(MainActivity.this, ""+result, 1).show();
                        ((TextView)findViewById(R.id.webservicecount))
        .setText(result);

        }
    }

并在onCreate中写下以下行

new DoInBack().execute("");

多数民众赞成