org.ksoap2.serialization.SoapSerializationEnvelope

时间:2013-12-24 08:51:14

标签: java android soap ksoap

任何人都可以帮我解决此错误吗?

我已完成所有设置...在java构建路径中也选择了jar文件..我的文件夹也仅以libs的名义...当我使用登录按钮将我的web服务与我的Android应用程序连接时错误总是显示出来....

我的应用包含过期证书网页的证书问题..我添加了代码以绕过证书错误...

但是当我点击登录按钮时,总会出现这个SoapSerialization错误。

这是我的代码

    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(); 
                 }
             }
        }
        }
    });

}

}

具有androidHTTPTransport.call(SOAP_ACTION,envelope)行的代码;从这一行代码不起作用......

logcat显示如下错误

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

2 个答案:

答案 0 :(得分:0)

转到项目/属性/ Java构建路径/订单和导出 - 如果您使用它,请确保在Android Dependencies和支持库前面进行检查。标记所有复选框并单击“应用”并清除项目。

答案 1 :(得分:0)

((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();
    }
}

这个web服务将获取json数据,这就是我在json中传递数据的原因。所以你必须检查命名空间是什么的三个东西,xml或json数据,FromCurrency.setName(“strInputData”);这是#c开发人员函数参数名称和互联网需求。