Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I'm creating a simple android application which talks to an asp.net web service hosted on my local machine's IIS server. I'm trying to access the web service using the ip "127.0.0.1".
Though no compile time error, my code throws a run time error "java.net.SocketTimeoutException". I tried debugging my code but can't really trace what exactly is wrong. Please help me out with it.

My web service connects to DB and pulls out the name for the id passed. I tested the Web service in browser it works fine.

Also there is a question.
1.) Can we connect to a web service hosted in localhost IIS server?

Below is my MainActivity.java code. Please can anyone check on it and provide me a solution or hint to move ahead. I googled almost for 13 days now but didn't worked for me. Help is really appreciated and thanks in advance

package com.example.getinfo;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity 
{
	
	private static final String SOAP_ACTION = "http://tempuri.org/getInfo";
    private static final String OPERATION_NAME = "getInfo";
    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
    private static final String SOAP_ADDRESS ="http://127.0.0.1:15701/GetInfo.asmx";
    //private static final String SOAP_ADDRESS = "http://127.0.0.1:11970/DBConnect/GetInfo.asmx";
    
    TextView txtData;
    EditText editData;
    Button btnGetData;
    int id;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		txtData = (TextView) findViewById(R.id.txtDisplay);
		btnGetData = (Button) findViewById(R.id.btnGetName);
		
		btnGetData.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View view) 
			{
				SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
				
				 PropertyInfo propertyInfo = new PropertyInfo();
                 propertyInfo.setName("id");
                 propertyInfo.setValue(id);
                 propertyInfo.setType(PropertyInfo.INTEGER_CLASS);
                 
                 editData =(EditText)findViewById(R.id.txtID);
                 id = Integer.parseInt(editData.getText().toString());
                 
                 request.addProperty(propertyInfo);
                 
                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                 envelope.dotNet = true;
                 
                 envelope.setOutputSoapObject(request);
                 
                 HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
                 
                 try  
                 {                    
                     httpTransport.call(SOAP_ACTION, envelope);                    
                     Object response = envelope.getResponse();                    
                     txtData.setText(response.toString());                    
                 }  
                 catch (Exception exception)   
                 {
                     txtData.setText(exception.toString()+"  Or enter number is not Available!");                    
                 }
                 
                 txtData = (TextView)findViewById(R.id.txtDisplay);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
Posted

1 solution

Hello Sujeet,

The localhost/127.0.0.1 in your code refers to the device on which the code is running, in this case the emulator. Try using the IP address of the machine on which the service is hosted. More information can be found here[^]

Regards,
 
Share this answer
 
Comments
Sujeet KC 16-May-14 3:55am    
Hi Prasad,

Thanks for giving me the tip regarding the ip thing. Now i tried it using the system ip i.e "192.168.1.108:15701/GetInfo.asmx". But im still getting the same error.

Thanks and regards,
Sujeet
Prasad Khandekar 16-May-14 4:07am    
Hello Sameer,

Just ensure that following line is present in your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission><

Regards,
Sujeet KC 16-May-14 4:49am    
Hello Prasad,

The line is present in AndroidManifest.xml. Also i have included android permissions for below

android:name="android.permission.INTERNET"
android:name="android.permission.ACCESS_WIFI_STATE"

Thanks again
Prasad Khandekar 16-May-14 5:18am    
Hello Sameer,

Are you able to invoke this service via SOAPUI (http://www.soapui.org/) Just to ensure that the service does not have any errors.

Regards,
Sujeet KC 16-May-14 5:39am    
Hello Prasad,

I'll try it right away and let you know, Thanks

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