• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Connecting to Local PC web service.

Ulargrind

Lurker
Jul 2, 2013
3
0
I have an android app that must connect to my local webservice with soap but I cant seem to get it working. This is the code I have.

[HIGH]public void Hello()
{
String SOAP_ACTION = "http://tempuri.org/Hello";
String METHOD_NAME = "Hello";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://10.0.2.2/MainService/MainService.asmx";

try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);

HttpTransportSE transport= new HttpTransportSE(URL);

transport.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

MyText.setText(resultString.toString());
}catch(Exception ex) {
MyText.setText("Error: " + ex.getMessage());
}
}[/HIGH]

All this does is catch an exception with the message "Null".
Note that I am not an android developer, this is my very first project.

Can anyone tell me what I did wrong. Thanks is advance.

P.S I did the same thing with Netbeans and regular Java and it works perfectly.
 
I found the problem. Turns out you cannot make network calls on the main thread.

[HIGH]
public void GetGenres()
{
SOAP_ACTION = "http://tempuri.org/getGenres";
METHOD_NAME = "getGenres";
NAMESPACE = "http://tempuri.org/";
URL = "http://192.168.1.83/MainService/MainService.asmx?op=getGenres";

Thread networkThread = new Thread() {
@Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

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

HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);

final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
final String str = response.toString();

runOnUiThread (new Runnable(){
public void run() {
Spinner TempList = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_list_item_1, str.split(","));
TempList.setAdapter(adapter);
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
}
[/HIGH]

For anyone else struggling with this.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones