开发者

ksoap2 connection timeout

I am trying to call a .net web service from an android emulator but I am getting connection timeout exception.

Before I tried to call my webService I called the http://www.w3schools.com/webservices/tempconvert.asmx without problems.

So I am assuming that either I have something wrong in my .net webService either in android application.

the .net is

 [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Pros : System.Web.Services.WebService {

    [WebMethod]
    public List<Product> Get(int pageIndex,int pageSize) {
        var efUnitOfWork = new EFUnitOfWork();
        var productsRepos = new ProductRepository(new EFRepository<Product>(), efUnitOfWork);
        return (List<Product>)productsRepos.GetByPage(pageIndex, pageSize);
    }
}

and the java is

private static final String METHOD_NAME = "Get";
private stati开发者_如何学Goc final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.0.2:2807/webservices/webService.asmx";
private static String SOAP_ACTION = NAMESPACE+METHOD_NAME;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

        request.addProperty("pageIndex", "0");
        request.addProperty("pageSize", "10");

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

        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result=(SoapObject)envelope.getResponse();

        String resultData=result.getProperty(0).toString();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

Am I missing something?


You can try to increase the timeout for the call like this:

AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL, 15000);


You should try to increase the timeout as mentioned but more importantly you have to move your call into a background thread. Use AsyncTask.

Also as discussed in the comment there is a blog post linked in the ksoap2-android wiki that explains the connection stuff in detail. You can NOT use localhost or equivalent since that is on the device but your service runs on a server.. now that I look at the code again .. 10.0.0.2 will not work most likely.

Do an ifconfig/ipconfig on the server and use that IP address. I bet it will work then.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜