-->

org.ksoap2.serialization.SoapSerializationEnvelope

2019-01-23 22:02发布

问题:

Can any one help me out to clear this error?

I have done all the settings... Selected the jar file in the java build path also.. my folder also in the name of libs only... when I connect my webservice with my android app using a login button the error shows it up always....

my app contains the certificate problem with a expired certificate webpage.. and I added the code to bypass the certificate error...

But this SoapSerialization error always occurs when I click the login button..

and here Is my code

    package com.test.mqilynx;


import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

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.os.StrictMode;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
   StrictMode.setThreadPolicy(policy);
}
Button loginb = (Button) findViewById(R.id.button1);
loginb.setOnClickListener( new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText uname = (EditText) findViewById(R.id.editText1);
EditText pwd = (EditText) findViewById(R.id.editText2);
String ustr = uname.getText().toString();
        String pstr = pwd.getText().toString();
        if (ustr.isEmpty() && pstr.isEmpty()){
            Toast to = Toast.makeText(MainActivity.this,"Please Login credentials",Toast.LENGTH_SHORT);
            to.setGravity(Gravity.CENTER, 0, 0);
            to.show();
        }
        else
        { if (ustr.isEmpty() || pstr.isEmpty())
            {
             if(ustr.isEmpty()){
             Toast to = Toast.makeText(MainActivity.this,"Please Enter Username",Toast.LENGTH_SHORT);
            to.setGravity(Gravity.CENTER, 0, 0);
            to.show();}
            else{
                Toast to = Toast.makeText(MainActivity.this,"Please Enter Password",Toast.LENGTH_SHORT);
                to.setGravity(Gravity.CENTER, 0, 0);
                to.show();
            }
                }
             else 
             {
                // Toast.makeText(getBaseContext(),"Welcome! " + ustr + " Pwd: "+ pstr,Toast.LENGTH_SHORT).show();


                 String METHOD_NAME = "Login";
                 String NAMESPACE = "https://www.qilynx.com/loadsoap.php";
                 String SOAP_ACTION = "https://www.qilynx.com/loadsoap.php/Login";
                // String URL = "https://auth:1qaz2wsx3edc4rfv@www.qilynx.com/loadsoap.php?desc";
                 String URL = "https://www.qilynx.com:443/loadsoap.php";
                 SoapObject userReq = new SoapObject(NAMESPACE,METHOD_NAME);
                 userReq.addProperty("user",ustr);
                 userReq.addProperty("pass",pstr);
                 System.out.println("user: " + "" + ustr + ":" + "Pwd: " + pstr);

            //   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //   request.addProperty("Login", userReq);

                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                // envelope.bodyOut = request;
                 envelope.dotNet=true;
                 envelope.setOutputSoapObject(userReq);
                 Log.e("Value of Req", userReq.toString());
                 Log.e("Value of Envelope", envelope.toString());
                // Log.i("bodyenv", envelope.bodyOut.toString());

                 HttpTransportSE androidHTTPTransport = new HttpTransportSE(URL);

                 //By-passing certificate - START
                // Create a trust manager that does not validate certificate chains



                 TrustManager[] trustAllCerts = new TrustManager[]{
                     new X509TrustManager() {
                         public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                             return null;
                         }
                         public void checkClientTrusted(
                             java.security.cert.X509Certificate[] certs, String authType) {
                         }
                         public void checkServerTrusted(
                             java.security.cert.X509Certificate[] certs, String authType)  {
                         }
                     }
                 };

                 // Install the all-trusting trust manager
                 try {
                     SSLContext sc = SSLContext.getInstance("SSL");
                     sc.init(null, trustAllCerts, new java.security.SecureRandom());
                     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                 } catch (Exception e) {
                     e.printStackTrace();
                 }


                 // By-passing certificate - END
                 try{
                     System.out.println("I AM IN TRY");
                     System.out.println("envelope1 "+envelope);
                     androidHTTPTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                     System.out.println("Before Bp1");
                     System.out.println("Bp 1");
                     androidHTTPTransport.call(SOAP_ACTION,envelope);
                     System.out.println("after Bp1");
                     System.out.println("Bp 2");
                     SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
                        Log.v("TAG",String.valueOf(resultsRequestSOAP));

                     System.out.println("envelope2 "+envelope); 
                     System.out.println("before resp");
                     SoapObject resp = (SoapObject) envelope.getResponse();
                     Log.i("return", resp.toString());
                     boolean check = Boolean.parseBoolean(resp.getProperty("result").toString());
                     if(check)
                     {
                         System.out.println("SUCCESS LOGIN");
                         /*Toast to = Toast.makeText(MainActivity.this,"Logged In",Toast.LENGTH_SHORT);
                            to.setGravity(Gravity.CENTER, 0, 0);
                            to.show(); */
                     }

                     else
                     {
                         /*Toast to = Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_SHORT);
                            to.setGravity(Gravity.CENTER, 0, 0);
                            to.show();*/
                         System.out.println("LOGIN FAILED");
                     }

                 }

                 catch(Exception e)
                 {
                     System.out.println("I AM IN ERROR");
                    e.printStackTrace(); 
                 }
             }
        }
        }
    });

}

}

The code which have the line androidHTTPTransport.call(SOAP_ACTION,envelope); from this line the code is not working...

the logcat showing the error like below

12-24 06:17:13.968: E/Value of Envelope(13752): org.ksoap2.serialization.SoapSerializationEnvelope@417c87b0

回答1:

Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.Mark all checkboxes and Click on Apply and clean the project.



回答2:

((Button)findViewById(R.id.btnData)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            NAMESPACE = "http://tempuri.org/";
            URL = "http://YOURURL/Service.asmx";
            SOAP_ACTION = "http://tempuri.org/SetLoginData";
            METHOD_NAME = "SetLoginData";
            EditText uname = (EditText) findViewById(R.id.editText1);
            EditText pwd = (EditText) findViewById(R.id.editText2);       

            String dataToSend="[{'UserID':uname.getText().toString() ,'Password':pwd.getText().toString() }]";

            SendData(dataToSend);
        }
    });
}

public void SendData(String data){

    Log.d(TAG, "SendData");
    Log.d(TAG, "URL   : "+URL);
    Log.d(TAG, "ACTION: "+SOAP_ACTION);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    Log.d(TAG, "Create");
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    String dataToSend=data;
    Log.d(TAG, "dataToSend: "+dataToSend);

    PropertyInfo FromCurrency = new PropertyInfo();
    FromCurrency.setName("strInputData");
    FromCurrency.setValue(dataToSend);
    FromCurrency.setType(String.class);
    request.addProperty(FromCurrency);

    Log.d(TAG, "envelope");
    // Create the envelop.Envelop will be used to send the request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.implicitTypes=true;
    envelope.setAddAdornments(false);

    envelope.setOutputSoapObject(request);
    // Says that the soap webservice is a .Net service
    envelope.dotNet = true;

    Log.d(TAG, "URL");
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        Log.d(TAG, "call");
        androidHttpTransport.call(SOAP_ACTION, envelope);
        Log.d(TAG, "call1");
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        Log.d(TAG,"response:"+ response.toString());
        TextView v = (TextView) findViewById(R.id.currency);
        v.setText("Retururn Responce" +  "= " + response.toString() ); 
    } catch (Exception e){
        e.printStackTrace();
    }
}

This webservice will take json data that's why i had passed data in json. so you have to check two three things that namespace is what, xml or json data , FromCurrency.setName("strInputData"); which is #c developer function argument name and internet permision requried.