2014-09-05 85 views
0

我正在創建一個Android應用程序,我必須連接來自我公司的Web服務來執行登錄系統,我正在使用3.3.0 KSOAP2庫。通過Android訪問Webservice - KSOAP2

我WS的路徑,例如:http://www.dominio.com/servicos/ws.asmx

我的問題發生:存儲連接

09-04 10:57:52.835: W/System.err(19717): 
SoapFault - faultcode: 'soap:Client' faultstring: 
'Server did not recognize the value of HTTP Header SOAPAction: 
http://www.dominio.com/servicos/ObterIdentificadorLoja.' 
faultactor: 'null' detail: [email protected] 

變量:

private final String NAMESPACE = "http://tempuri.org/"; 
private final String URL = "http://www.dominio.com/servicos/ws.asmx"; 
private final String SOAP_ACTION = "http://tempuri.org/ObterIdentificadorLoja"; 
private final String METHOD_NAME = "ObterIdentificadorLoja"; 
private String TAG = "LOGAR"; 

我WSDL是:

<wsdl:operation name="ObterIdentificadorLoja"> 
<soap:operation soapAction="http://tempuri.org/ObterIdentificadorLoja" style="document"/> 

源代碼的.java全:

package com.testes.infovendas; 

//Imports 
import android.support.v7.app.ActionBarActivity; 
import android.os.AsyncTask; 
import android.os.Bundle; 

//KSOAP2 -- Lib de conexão Webservice SOAP 
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 

//Adicionais 
import com.testes.infovendas.R; 

import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class InfoVendas extends ActionBarActivity { 

//Variáveis 
private final String NAMESPACE = "http://tempuri.org/"; 
private final String URL = "http://www.dominio.com/servicos/ws.asmx"; 
private final String SOAP_ACTION = "http://tempuri.org/ObterIdentificadorLoja"; 
private final String METHOD_NAME = "ObterIdentificadorLoja"; 
private String TAG = "ASSYNC"; 
private static String cnpj_cpf_codclie, codloja, seguranca, identificadorloja; 
Button b; 
TextView tv; 
EditText et1,et2,et3,et4; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    //Identificando cada campo e função 
    et1 = (EditText) findViewById(R.id.editCodloja); 
    et2 = (EditText) findViewById(R.id.editCPF); 
    et3 = (EditText) findViewById(R.id.editUser); 
    et4 = (EditText) findViewById(R.id.editPass); 
    tv = (TextView) findViewById(R.id.login_informa); 
    b = (Button) findViewById(R.id.btnEnviar); 
    //Listener para quando clicar no botão Enviar 
    b.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      //Se todos os campos forem preenchidos, será retomado o login 
      if (et1.getText().length() != 0 && et1.getText().toString() != "" || 
        et2.getText().length() != 0 && et2.getText().toString() != "" || 
        et3.getText().length() != 0 && et3.getText().toString() != "" || 
        et4.getText().length() != 0 && et4.getText().toString() != "") { 

       //Pega todas as informações escritas nos campos e adiciona em suas respectivas variáveis para uso do WS 
       codloja = et1.getText().toString(); 
       cnpj_cpf_codclie = et2.getText().toString(); 
       seguranca = "chavesecreta"; 

       AsyncCallWS task = new AsyncCallWS(); 
       task.execute(); 

      } 
      //Se não for preenchido todos os campos, será retornado um TextView apenas informado para informar corretamente 
      else { 
       tv.setText("Por favor, insira todos os dados."); 
      } 
     } 
    }); 
} 

//Classe AsyncCallWS 
private class AsyncCallWS extends AsyncTask<String, Void, Void> { 

    //Retorna como null o valor do Identificador 
    @Override 
    protected Void doInBackground(String... params) { 
     Log.i(TAG, "doInBackground"); 
     getIdentificador(cnpj_cpf_codclie, codloja, seguranca); 
     return null; 
    } 

    //Mensagem e ação pós conclusão 
    @Override 
    protected void onPostExecute(Void result) { 
     Log.i(TAG, "onPostExecute"); 
     tv.setText("identificador nº: " + identificadorloja + " Conexão estabelecida. Realizando o login..."); 
    } 

    //Mensagem ao clicar no botão Enviar 
    @Override 
    protected void onPreExecute() { 
     Log.i(TAG, "onPreExecute"); 
     tv.setText("Estabelecendo conexão ao servidor..."); 
    } 


    @Override 
    protected void onProgressUpdate(Void... values) { 
     Log.i(TAG, "onProgressUpdate"); 
    } 

} 

//Classe para obter os dados do Identificador 
public void getIdentificador(String cnpj_cpf_codclie, String codloja, String seguranca) { 
    //Create request 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    //Property which holds input parameters 
    PropertyInfo identificadorPI = new PropertyInfo(); 
    //Set Name 
    identificadorPI.setName("identificadorloja"); 
    //Set Value 
    identificadorPI.setValue(identificadorloja); 
    //Set dataType 
    identificadorPI.setType(double.class); 
    //Add the property to request object 
    request.addProperty(identificadorPI); 
    //Create envelope 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
      SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    //Set output SOAP object 
    envelope.setOutputSoapObject(request); 
    //Create HTTP call object 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    try { 
     //Invoke web service 
     androidHttpTransport.call(SOAP_ACTION, envelope); 
     //Get the response 
     SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 
     //Define identificadorloja como uma variável estática 
     identificadorloja = response.toString(); 

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

} 

我需要訪問「ObterIdentificadorLoja」,只是總是給人錯誤或「頭無法識別」或「超時」或「參考未設置爲一個實例或對象「,並且我已經嘗試了幾種方法。

我可以在哪裏失蹤?

回答

0

由於您的錯誤明確指出,您有SOAPAction不匹配。

您正在使用http://www.dominio.com/servicos/ObterIdentificadorLoja同時使SOAP請求,

,但是,在你的WSDL,你有http://tempuri.org/ObterIdentificadorLoja

+0

我已經變了!對不起,但問題仍然存在.. – 2014-09-05 16:07:40

+0

我改變了我的問題!看看,@ bhargavg! – 2014-09-05 16:38:38