Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all
i try to connect to local host asmx webservice that insert data into local database from mobile application using android studio .

i received error
java.net.connectexception connection refused connect


can i connect to localhost webservice from android code , by the way my webservice work fine after run from visual studio

What I have tried:

this is my code in android studio :

=====class = =====
public class ContactResult {

    public  static  int ErrorID  ;
    public static String ErrorMessage;
}


====== class 2 ==============
public class CallWebservice {

    private final  String NAMESPACE= "http://tempuri.org/";
    private  final  String URL = "http://localhost:1844/AndroidWebService.asmx";


    public void OpenAccount(String FullName , String Phone) {
        final String METHOD_NAME = "OpenAccount";
        final String SOAP_ACTION = "http://tempuri.org/OpenAccount";
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("FullName", FullName);
        request.addProperty("Phone", Phone);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        Object response = null;
        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject result = (SoapObject) envelope.getResponse();
            ContactResult.ErrorMessage = result.getProperty("ErrorMessage").toString();
            ContactResult.ErrorID = Integer.parseInt(result.getProperty("ErrorID").toString());
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }


======== this code inside mainActivity ======================
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }

  public void addclick(View view) {

      final EditText txtname  = (EditText)findViewById(R.id.txtname);
      final EditText txtphone  = (EditText)findViewById(R.id.txtphone);

      Thread runt = new Thread(){
          public void run(){
              CallWebservice cweb  = new CallWebservice();
              cweb.OpenAccount(txtname.getText().toString() , txtphone.getText().toString());
          }
      };
      runt.start();
      try {
          runt.join();

      }
      catch (Exception ex){}
  }
Posted
Updated 2-Jun-19 23:59pm
Comments
F-ES Sitecore 3-Jun-19 5:24am    
"localhost" means the machine the code is currently running on. If this code is running on your desktop and the service is running inside an IIS host then it will work. If the code is on an android device then your service would also need to be running on a webhost on the android device too. If you intend the service to live somewhere outside of the android device then it needs to be at a host that the android device can resolve\access and the code will need to be updated to access that host rather than "localhost".

1 solution

'locahost', by definition is local to the computer it runs on...
If you are intend to access any web service from a phone, you have to host it in the public, so it will be accessible from everywhere, just like CP accessible from everywhere...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900