Android:无法使用Ksoap2连接到Web服务

时间:2013-02-16 20:43:43

标签: java android eclipse ksoap2 android-ksoap2

当我发送用户名和密码时,它发送给我一个错误:publicKey和用户名和密码没有连接Web服务,我不知道是否有任何问题 http://mobilews.terra.net.lb/MobileService.asmx?op=authenticateLogin 用户名是:mabyad 密码是:dori

MainActivity.java代码

package com.kak;

import android.os.Bundle;
import android.os.StrictMode;

import com.kak.R;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//import android.widget.Toast;
//import android.view.View.OnClickListener;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.serialization.SoapPrimitive;
//import android.widget.TextView;
//import android.app.ProgressDialog;
//import android.os.AsyncTask;


public class MainActivity extends Activity {
    private final static String SOAP_NAMESPACE = "http://mobilews.terra.net.lb";  

    private static final  String SOAP_URL = "http://mobilews.terra.net.lb/MobileService.asmx";  

     private static final  String SOAP_ACTION = "http://tempuri.org/authenticateLogin";  

    private static final  String SOAP_METHOD_NAME = "authenticateLogin"; 
    //private PropertyInfo pi1; 


    Button button1;
    EditText UserName;
    EditText Password;
    public String publicKey;

  String tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = 
                new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        setContentView(R.layout.activity_main);

        button1=(Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() 
        {


            @Override

            public void onClick(View v) 
            {
                UserName=(EditText)findViewById(R.id.UserName);
                Password = (EditText)findViewById(R.id.Password);

                SoapObject request = new SoapObject(SOAP_NAMESPACE, SOAP_METHOD_NAME); 


            request.addProperty("userName", UserName.getText().toString());
                request.addProperty("password",Password.getText().toString());
                request.addProperty("publicKey", String.valueOf("Terr@Net$M0bilePK54"));


                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                 envelope.setOutputSoapObject(request);
                 envelope.dotNet = true;


                     HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_URL);
                     try{

                     androidHttpTransport.call(SOAP_ACTION, envelope);

                     SoapPrimitive resultString = (SoapPrimitive)envelope.getResponse();



![android manifest Permissions][1]                 
                     SoapPrimitive resultInteger =(SoapPrimitive)envelope.getResponse();

                     tv=resultInteger.toString(); 

                    if (resultString!=null || resultInteger !=null) {
                        Toast.makeText(getBaseContext(),tv,
                                Toast.LENGTH_LONG).show();
                    }   
                    else {
                        Toast.makeText(getBaseContext(),"wrong",
                            Toast.LENGTH_LONG).show();
                   }

                 }catch (Exception e) {

                         e.printStackTrace();

                   }
            }
            });
        }











    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

我使用了ksoap2-android-assembly-2.6.2-jar-with-dependencies.jar,这段代码对我有用:

public class MainActivity extends Activity {
    private final String URL = "http://mobilews.terra.net.lb/MobileService.asmx";
    private final String METHOD_NAME = "authenticateLogin";
    private final String NAMESPACE = "http://tempuri.org/";
    private final String SOAP_ACTION = NAMESPACE + METHOD_NAME;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        authenticateLogin("mabyad", "dori", "your_public_key");
    }

    private void authenticateLogin(String userName, String passWord, String publicKey) {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("userName", userName);
        request.addProperty("password", passWord);
        request.addProperty("publicKey", passWord);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject) envelope.bodyIn;

            boolean authenticateLoginResult = Boolean.parseBoolean(response.getPropertyAsString("authenticateLoginResult"));
            int customerId = Integer.parseInt(response.getPropertyAsString("customerId"));
            String errorMsg = response.getPropertyAsString("errorMsg");

            System.out.println("authenticateLoginResult :: " + authenticateLoginResult);
            System.out.println("customerId :: " + customerId);
            System.out.println("errorMsg :: " + errorMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出是:

  

authenticateLoginResult :: false

     

customerId :: 0

     

errorMsg :: unauthirized方法用法,无效密钥