I have the following code:
public class DotNetWSActivity extends AppCompatActivity {
Button b;
TextView tv;
EditText et;
ProgressBar pg;
String editText;
String displayText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Name Text control
et = (EditText) findViewById(R.id.editText1);
//Display Text control
tv = (TextView) findViewById(R.id.tv_result);
//Button to trigger web service invocation
b = (Button) findViewById(R.id.button1);
//Display progress bar until web service invocation completes
pg = (ProgressBar) findViewById(R.id.progressBar1);
//Button Click Listener
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//empty
if (et.getText().length() != 0 && et.getText().toString() != "") {
//Get the text control value
editText = et.getText().toString();
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
//If text control is empty
} else {
tv.setText("Camp buït");
}
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
//Invoke webservice
displayText = WebService.invokarGetempresa(editText,"KBXGetEmpresa");
return null;
}
@Override
protected void onPostExecute(String result) {
//Set response
tv.setText(displayText);
//Make ProgressBar invisible
pg.setVisibility(View.INVISIBLE);
}
@Override
protected void onPreExecute() {
//Make ProgressBar invisible
pg.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
public class WebService {
//Namespace of the Webservice - can be found in WSDL
private static String NAMESPACE = "http://tempuri.org/";
//Webservice URL - WSDL File location
private static String URL = "http://01.westeurope.cloudapp.azure.com:8080/SGAWebservice.asmx?op=KBXGetEmpresa";
//SOAP Action URI again Namespace + Web method name
private static String SOAP_ACTION = "http://tempuri.org/";
public static String invokarGetempresa(String aCodigoEmpresa, String KBXGetEmpresa) {
String resTxt = null;
// Create request
SoapObject request = new SoapObject(NAMESPACE, aCodigoEmpresa);
// Property which holds input parameters
PropertyInfo pi = new PropertyInfo();
// Set Name
pi.setValue("empresa");
// Set Value
pi.setValue(aCodigoEmpresa);
// Set dataType
pi.setType(String.class);
// Add the property to request object
request.addProperty(pi);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
// Invoke web service
androidHttpTransport.call(SOAP_ACTION + KBXGetEmpresa, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
// Assign it to resTxt variable static variable
resTxt = response.toString();
} catch (Exception e) {
//Print error
e.printStackTrace();
//Assign error message to resTxt
resTxt = "Error";
}
//Return resTxt to calling object
return resTxt;
}
}
I think I have the problem in the Asyn Task, but I'm not sure. I'm trying to consume a webservice but I can not get it.
It is a webservice that has the following query:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header>
<tem:UserCredentials>
<!--Optional:-->
<tem:userName>usuarioX</tem:userName>
<!--Optional:-->
<tem:password>1234</tem:password>
</tem:UserCredentials>
</soapenv:Header>
<soapenv:Body>
<tem:KBXGetEmpresa>
<!--Optional:-->
<tem:aCodigoEmpresa>101</tem:aCodigoEmpresa>
</tem:KBXGetEmpresa>
</soapenv:Body>
</soapenv:Envelope>
The only value that I need to fill out to CodigoEmpresa that I will put a number: for example 201 and I will return the name of this. Both values are String. I also need to put username and password so maybe that's why it does not work?
The error you give me is the following:
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.kxml2.io.KXmlSerializer.append(KXmlSerializer.java:75)
W/System.err: at org.kxml2.io.KXmlSerializer.startTag(KXmlSerializer.java:442)
at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:660)
at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:645)
at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:702)
at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:618)
at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:198)
W/System.err: at org.ksoap2.transport.Transport.createRequestData(Transport.java:107)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:119)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
at e.ma.myapplication.WebService.invokarGetempresa(WebService.java:43)
at e.ma.myapplication.DotNetWSActivity$AsyncCallWS.doInBackground(DotNetWSActivity.java:59)
at e.ma.myapplication.DotNetWSActivity$AsyncCallWS.doInBackground(DotNetWSActivity.java:55)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
InvokeGetEmpresa Method:
public static String invokarGetempresa(String aCodigoEmpresa, String KBXGetEmpresa, String password, String userName) {
String resTxt = null;
// Create request
SoapObject request = new SoapObject(NAMESPACE, aCodigoEmpresa);
// Property which holds input parameters
PropertyInfo pi = new PropertyInfo();
// Set Name
pi.setValue("empresa");
// Set Value
pi.setValue(aCodigoEmpresa);
// Set dataType
pi.setType(String.class);
// Add the property to request object
request.addProperty(pi);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapPrimitive resultsRequestSOAP = null;
try {
// Invoke web service
androidHttpTransport.call(SOAP_ACTION + KBXGetEmpresa, envelope);
// Get the response
resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
// Assign it to resTxt variable static variable
resTxt = resultsRequestSOAP.toString();
} catch (Exception e) {
//Print error
e.printStackTrace();
//Assign error message to resTxt
resTxt = "Error";
}
//Return resTxt to calling object
return resTxt;