调用Web服务时出错

时间:2013-04-26 05:22:18

标签: android web-services integration

我是android和Web Services(基于java soap)的初学者。我试图做的是从web服务请求其中一个方法。它不需要任何参数进行初始测试并捕获响应logcat或简单的文本视图。最初,它给了我身份验证错误,它提供了对Web服务的基本http身份验证,因为它需要相同,然后

当我再次运行程序时,它开始在我调用的这行之后抛出异常:

httpTransport.call(SOAP_ACTION, envelope);

我得到的错误是:

  

04-26 10:10:12.945:I / System.out(322):org.xmlpull.v1.XmlPullParserException:意外类型(位置:TEXT enter code here   04-26 10:10:12.945:I / System.out(322):------ = _ Part_56 ... @ 5:13 java.io.InputStreamReader@40580270)

我搜索了很多内容,甚至搜索了几乎所有堆栈中的问题。但没有找到任何有用的解决方案,因此再次提出这个问题。请仔细阅读代码并告诉我出错的地方

package com.webservices_demo;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Base64;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
/*working one*/
public  class FirstAppUI extends Activity {
private static final String SOAP_ACTION ="http://server name:portNo/ws/TririgaWS";
    private static final String OPERATION_NAME = "getApplicationInfo()";
    private static final String WSDL_TARGET_NAMESPACE = "http://ws.tririga.com/";
    private static final String SOAP_ADDRESS = "http://servername:port.no/ws/TririgaWS?wsdl";




    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        setContentView(textView);



        /*Basic Http Authentication to connect*/


Authenticator.setDefault(new Authenticator(){
 protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("system","admin".toCharArray());
                    }});
                HttpURLConnection c = null;
                try {
                c = (HttpURLConnection) new URL(SOAP_ADDRESS).openConnection();
                    System.out.println("=======Connection successful=======");
                //c.setRequestProperty("Authorization", "basic " +
                     //       Base64.encode("system:admin".getBytes(), 0));
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                e1.printStackTrace();


                }
                c.setUseCaches(false);
                try {
                    c.connect();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                }



        SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
        System.out.println("---------------This is Request sent-------------");
        System.out.println(request.toString());
        System.out.println("---------------End of Request sent--------------");
        SoapSerializationEnvelope envelope = new      SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);


        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

        try

        {
            ///for testing purpose
            System.out.println("======= Path Is=======" +httpTransport.getPath());
            System.out.println("======= Host Is======="+httpTransport.getHost());
            System.out.println("======= Port Is======="+httpTransport.getPort());
            System.out.println("======= String =======" +httpTransport.toString() );
            System.out.println("======= Debugging ======="+ httpTransport.debug);
            //end testing



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

            SoapObject response = (SoapObject) envelope.getResponse();

             System.out.println("result is:- "+ result.toString());
                //FirstAppUI[] getApplicationInfo = RetrieveFromSoap(response);

                TextView actv = (TextView)findViewById(R.id.textView);
                //textView.setHint(getApplicationInfo[0].toString());



            System.out.println("=======Connection Is======="+httpTransport.getConnection() );
            //SoapObject response = (SoapObject) envelope.getResponse();


            System.out.println("=======Namespace is=======" +response.getNamespace());  
            //Object obj = envelope.bodyIn; 
        textView.setText(response.toString());
            System.out.println("-------------------Body In Response---------------");

            System.out.println(response.toString());

        }

        catch (Exception exception)

        {

            textView.setText(exception.toString());
            String issue= exception.toString();
            System.out.println("====================================================CAUGHT");
            System.out.println(issue);
            exception.printStackTrace();


        }

    }


}

这是Logcat输出

04-26 10:10:04.454: D/installd(35): DexInv: --- BEGIN '/data/app/com.webservices_demo-2.apk' ---
04-26 10:10:05.064: D/dalvikvm(300): DexOpt: 'Lorg/kxml2/kdom/Node;' has an earlier definition; blocking out
04-26 10:10:05.064: D/dalvikvm(300): DexOpt: 'Lorg/xmlpull/v1/XmlPullParser;' has an earlier definition; blocking out
04-26 10:10:05.064: D/dalvikvm(300): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserException;' has an earlier definition; blocking out
04-26 10:10:05.064: D/dalvikvm(300): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserFactory;' has an earlier definition; blocking out
04-26 10:10:05.064: D/dalvikvm(300): DexOpt: 'Lorg/xmlpull/v1/XmlSerializer;' has an earlier definition; blocking out
04-26 10:10:05.094: D/dalvikvm(300): DexOpt: 'Lorg/kxml2/io/KXmlParser;' has an earlier definition; blocking out
04-26 10:10:05.094: D/dalvikvm(300): DexOpt: 'Lorg/kxml2/io/KXmlSerializer;' has an earlier definition; blocking out
04-26 10:10:05.094: D/dalvikvm(300): DexOpt: 'Lorg/kxml2/kdom/Document;' has an earlier definition; blocking out
04-26 10:10:05.094: D/dalvikvm(300): DexOpt: 'Lorg/kxml2/kdom/Element;' has an earlier definition; blocking out
04-26 10:10:05.544: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParserException;'
04-26 10:10:05.674: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParserException;'
04-26 10:10:05.714: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:05.714: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:05.725: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/kxml2/io/KXmlSerializer;'
04-26 10:10:05.725: D/dalvikvm(300): DexOpt: not verifying 'Lorg/kxml2/kdom/Node;': multiple definitions
04-26 10:10:05.734: D/dalvikvm(300): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParser;': multiple definitions
04-26 10:10:05.734: D/dalvikvm(300): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserException;': multiple definitions
04-26 10:10:05.734: D/dalvikvm(300): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserFactory;': multiple definitions
04-26 10:10:05.734: D/dalvikvm(300): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlSerializer;': multiple definitions
04-26 10:10:06.104: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:06.104: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:06.104: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:06.104: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:06.104: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:06.104: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:06.114: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
04-26 10:10:06.124: D/dalvikvm(300): DexOpt: not verifying 'Lorg/kxml2/io/KXmlParser;': multiple definitions
04-26 10:10:06.124: D/dalvikvm(300): DexOpt: not verifying 'Lorg/kxml2/io/KXmlSerializer;': multiple definitions
04-26 10:10:06.124: D/dalvikvm(300): DexOpt: not verifying 'Lorg/kxml2/kdom/Document;': multiple definitions
04-26 10:10:06.124: D/dalvikvm(300): DexOpt: not verifying 'Lorg/kxml2/kdom/Element;': multiple definitions
04-26 10:10:06.124: I/dalvikvm(300): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParserException;'
04-26 10:10:06.164: D/dalvikvm(300): DexOpt: load 167ms, verify+opt 1058ms
04-26 10:10:06.244: D/installd(35): DexInv: --- END '/data/app/com.webservices_demo-2.apk' (success) ---
04-26 10:10:06.244: W/PackageManager(61): Code path for pkg : com.webservices_demo changing from /data/app/com.webservices_demo-1.apk to /data/app/com.webservices_demo-2.apk
04-26 10:10:06.244: W/PackageManager(61): Resource path for pkg : com.webservices_demo changing from /data/app/com.webservices_demo-1.apk to /data/app/com.webservices_demo-2.apk
04-26 10:10:06.244: D/PackageManager(61):   Activities: com.webservices_demo.FirstAppUI com.webservices_demo.EarthQuake com.webservices_demo.Webservices_Demo
04-26 10:10:06.254: I/ActivityManager(61): Force stopping package com.webservices_demo uid=10037
04-26 10:10:06.364: I/installd(35): move /data/dalvik-cache/data@app@com.webservices_demo-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.webservices_demo-2.apk@classes.dex
04-26 10:10:06.364: D/PackageManager(61): New package installed in /data/app/com.webservices_demo-2.apk
04-26 10:10:06.484: I/ActivityManager(61): Force stopping package com.webservices_demo uid=10037
04-26 10:10:06.534: D/dalvikvm(168): GC_EXPLICIT freed 305K, 52% free 2778K/5703K, external 1625K/2137K, paused 52ms
04-26 10:10:06.604: I/ActivityManager(61): Start proc com.svox.pico for broadcast com.svox.pico/.VoiceDataInstallerReceiver: pid=301 uid=10009 gids={}
04-26 10:10:06.634: W/RecognitionManagerService(61): no available voice recognition services found
04-26 10:10:06.854: I/ActivityThread(301): Pub com.svox.pico.providers.SettingsProvider: com.svox.pico.providers.SettingsProvider
04-26 10:10:07.104: D/dalvikvm(61): GC_EXPLICIT freed 950K, 46% free 4569K/8455K, external 4373K/7373K, paused 108ms
04-26 10:10:07.114: I/installd(35): unlink /data/dalvik-cache/data@app@com.webservices_demo-1.apk@classes.dex
04-26 10:10:07.134: D/AndroidRuntime(216): Shutting down VM
04-26 10:10:07.144: D/dalvikvm(216): GC_CONCURRENT freed 100K, 72% free 293K/1024K, external 0K/0K, paused 2ms+1ms
04-26 10:10:07.154: D/jdwp(216): adbd disconnected
04-26 10:10:07.184: I/dalvikvm(216): JNI: AttachCurrentThread (from ???.???)
04-26 10:10:07.184: I/AndroidRuntime(216): NOTE: attach of thread 'Binder Thread #3' failed
04-26 10:10:07.774: D/AndroidRuntime(314): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
04-26 10:10:07.774: D/AndroidRuntime(314): CheckJNI is ON
04-26 10:10:08.344: D/AndroidRuntime(314): Calling main entry com.android.commands.am.Am
04-26 10:10:08.364: I/ActivityManager(61): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.webservices_demo/.FirstAppUI } from pid 314
04-26 10:10:08.394: I/ActivityManager(61): Start proc com.webservices_demo for activity com.webservices_demo/.FirstAppUI: pid=322 uid=10037 gids={3003}
04-26 10:10:08.404: D/AndroidRuntime(314): Shutting down VM
04-26 10:10:08.414: D/dalvikvm(314): GC_CONCURRENT freed 101K, 69% free 318K/1024K, external 0K/0K, paused 1ms+2ms
04-26 10:10:08.414: D/jdwp(314): adbd disconnected
04-26 10:10:08.494: I/dalvikvm(314): JNI: AttachCurrentThread (from ???.???)
04-26 10:10:08.494: I/AndroidRuntime(314): NOTE: attach of thread 'Binder Thread #3' failed
04-26 10:10:09.084: I/System.out(322): executing requestGET http://192.168.1.70:8001/ws/TririgaWS HTTP/1.1
04-26 10:10:11.920: I/System.out(322): ----------------------------------------
04-26 10:10:11.920: I/System.out(322): HTTP/1.1 200 OK
04-26 10:10:11.920: I/System.out(322): org.apache.http.message.BasicHttpResponse@40565650
04-26 10:10:11.925: I/System.out(322): Response content length: 47
04-26 10:10:11.935: I/System.out(322): =======Connection successful=======
04-26 10:10:12.335: I/System.out(322): ---------------This is Request sent-------------
04-26 10:10:12.335: I/System.out(322): getApplicationInfo(){}
04-26 10:10:12.335: I/System.out(322): ---------------End of Request sent--------------
04-26 10:10:12.365: I/System.out(322): ======= Path Is=======/ws/TririgaWS
04-26 10:10:12.365: I/System.out(322): ======= Host Is=======192.168.1.70
04-26 10:10:12.365: I/System.out(322): ======= Port Is=======8001
04-26 10:10:12.365: I/System.out(322): ======= String =======org.ksoap2.transport.HttpTransportSE@40571398
04-26 10:10:12.365: I/System.out(322): ======= Debugging =======false
04-26 10:10:12.945: I/System.out(322): ====================================================CAUGHT
04-26 10:10:12.945: I/System.out(322): org.xmlpull.v1.XmlPullParserException: unexpected type (position:TEXT 
04-26 10:10:12.945: I/System.out(322): ------=_Part_56...@5:13 in java.io.InputStreamReader@40580270)* 
04-26 10:10:12.945: W/System.err(322): org.xmlpull.v1.XmlPullParserException: unexpected type (position:TEXT 
04-26 10:10:12.955: W/System.err(322): ------=_Part_56...@5:13 in java.io.InputStreamReader@40580270) 
04-26 10:10:12.955: W/System.err(322):  at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
04-26 10:10:12.955: W/System.err(322):  at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:1413)
04-26 10:10:12.955: W/System.err(322):  at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:126)
04-26 10:10:12.955: W/System.err(322):  at org.ksoap2.transport.Transport.parseResponse(Transport.java:96)
04-26 10:10:12.955: W/System.err(322):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:189)
04-26 10:10:12.955: W/System.err(322):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
04-26 10:10:12.955: W/System.err(322):  at com.webservices_demo.FirstAppUI.onCreate(FirstAppUI.java:196)
04-26 10:10:12.955: W/System.err(322):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 10:10:12.955: W/System.err(322):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-26 10:10:12.965: W/System.err(322):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-26 10:10:12.965: W/System.err(322):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-26 10:10:12.965: W/System.err(322):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-26 10:10:12.965: W/System.err(322):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 10:10:12.965: W/System.err(322):  at android.os.Looper.loop(Looper.java:123)
04-26 10:10:12.965: W/System.err(322):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-26 10:10:12.965: W/System.err(322):  at java.lang.reflect.Method.invokeNative(Native Method)
04-26 10:10:12.965: W/System.err(322):  at java.lang.reflect.Method.invoke(Method.java:507)
04-26 10:10:12.965: W/System.err(322):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-26 10:10:12.965: W/System.err(322):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-26 10:10:12.965: W/System.err(322):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:-1)

试试此代码

try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                androidHttpTransport.call(SOAP_ACTION, envelope);

                SoapObject result = (SoapObject) envelope.bodyIn;

                if (result != null) {

                tvResult.setText(result.getProperty0).toString());

                } else {

                    tvResult.setText("No Output");
                }

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                tvResult.setText(e.toString());
            }

希望这会有所帮助。