Question

I'm trying to call a web service from my Android app. I deployed a web service that inserts a record (product barcode, product name) with Hibernate in Java and I wrote an activity to test it.

When I run it on my AVD I got a connectionException : Connection refused

Here is my code:

package com.market_helper.app;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.serialization.PropertyInfo;

public class Market_helper_androidActivity extends Activity {

    private static final String SOAP_ACTION = "http://localhost:7675/market_helper/services/Main";
    private static final String METHOD_NAME = "insertProduct";
    private static final String NAMESPACE = "http://localhost:7675/market_helper/services/";
    private static final String URL = "http://localhost:7675/market_helper/services/Main?wsdl";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
           request.addProperty("barCode", "12345");

           request.addProperty("productName", "abc");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = envelope.getResponse();


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

It's my first webservice on Android and I'm not sure if the code is right. I'm calling a method that called insertProduct(String barCode,String productName). The web service is working and tested.

Was it helpful?

Solution

private static final String SOAP_ACTION = "http://localhost:7675/market_helper/services/Main"; 

You have given LocalHost

try with ip address instead of LocalHost

lik this

   private static final String SOAP_ACTION = "http://10.120.159.87:7675/market_helper/services/Main"; 

OTHER TIPS

In your AndroidManifest.xml be sure you have Internet permissions set, I was having the same issue once I set this everything worked great.

<uses-permission android:name="android.permission.INTERNET" />
private static final String URL = "http://localhost:7675/market_helper/services/Main?wsdl";

in this line it is wrong to put "?wsdl" at the end of the URL remove it and put extension(.asmx or .svc) of ur webservice at the end instead of it.Like...

private static final String URL = "http://localhost:7675/market_helper/services/Main.asmx";

Thanks for helping me I have fixed my problem with the url
I used the ipconfig command to get my local ip
the port 7675 is used by my apache tomcat server
I added the port to my firewall exception port to be sure that it works .

and there is no asmx file I changed ?wsdl
now I can see the wsdl description from my browser.

I have got the namespace is from the xml file "main.wsdl"
<schema elementFormDefault="qualified" targetNamespace="http://app.market_helper.com" xmlns="http://www.w3.org/2001/XMLSchema">

the correct code is here : private static final String SOAP_ACTION = "http://app.market_helper.com/insertProduct"; private static final String METHOD_NAME = "insertProduct"; private static final String NAMESPACE = "http://app.market_helper.com"; private static final String URL = "http://192.168.1.2:7675/market_helper/services/Main?wsdl";
now I can use the web service now from my Android app .
I send back my correct code for people who have the same problem as me .

I can see when I open explorer. I use this website

"http:// local host:49674/Service.svc"

What should I write? I always get error in try catch. I used WCF in .net webservice. and EntityFramework.

     private final String NAMESPACE="http://tempuri.org/IService/";
     private final String SOAPACTION="http://tempuri.org/IService/GetTreatmentValues";
     private final String METHODNAME="GetTreatmentValues";    
    private final String URL="http:// local host:49674/Service.svc";

where is my error ? (Also I use ksoap 2.4)

private static final String SOAP_ACTION = "http://localhost:7675/market_helper/services/insertProduct"; 

SOAP ACTION=NAMESPACE+METHOD NAME;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top