Reputation: 59
In my code there is a webservice call where webmethod is to simply display astring. I am running the webservice as localhost, but its not working.Ithink the problem is calling webservice from the main thread itself.please can anyone help to call it from a different thread
SoapTestActivity.java
package com.sample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;
public class SoapTestActivity extends Activity {
TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (TextView)findViewById(R.id.result);
final String NAMESPACE = "http://sample.com/";
final String METHOD_NAME = "SayHello";
final String SOAP_ACTION = "http://sample.com/SayHello";
final String URL = "http://192.168.1.104/HelloAndroid/Service1.asmx";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
String resultValue = response.toString();
result.setText(resultValue);
}
catch (Exception e) {
result.setText(e.getMessage());
}
}
}
Service1.asmx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
namespace HelloAndroid
{
[WebService(Namespace = "http://sample.com/")]
public class Service1 : System.Web.Services.WebService
{ [WebMethod]
public string SayHello() {
return "Hello, Android from .NET";
}
}
}
Upvotes: 1
Views: 572
Reputation: 11766
You can use an aync-task for this follow the blow code
class myAsyncTask extends AsyncTask<Void,Void,String>
{
protected void doInBackground(Void...params)
{
final String NAMESPACE = "http://sample.com/";
final String METHOD_NAME = "SayHello";
final String SOAP_ACTION = "http://sample.com/SayHello";
final String URL = "http://192.168.1.104/HelloAndroid/Service1.asmx";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
String resultValue = response.toString();
}
catch (Exception e) {
result.setText(e.getMessage());
}
return resultValue;
}
protected void onPostExcecute(String result)
{
super.onPostExecute(result);
result.setText(result);
}
}
now call this Async task by creating an object in MainActivity like myasyncTask object =new myAsyncTask();
and now execute it like object.execute();
Upvotes: 0
Reputation: 1090
You are not advised to make a network call in the main thread of your application, For this you need to use an Async-task. refer this
sample AsyncTask class
private class NetworkTask extends AsyncTask<Void, Void, Void> {
final String NAMESPACE = "http://sample.com/";
final String METHOD_NAME = "SayHello";
final String SOAP_ACTION = "http://sample.com/SayHello";
final String URL = "http://192.168.1.104/HelloAndroid/Service1.asmx";
String resultValue ="";
protected Void doInBackground(Void... urls) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
resultValue = response.toString();
}
catch (Exception e) {
resultValue = e.getMessage();
}
}
protected void onPostExecute(Long result) {
result.setText(resultValue);
}
}
Invoke this asynctask in onCreate as-
new NetworkTask().excecute();
Upvotes: 2