• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Http Connection refused on calling WebAPI

LotusShiv

Lurker
1. I have developed a Web API using VS2012 and .Net Framework 4.5. Its hosted on IIS on a separate WebSite (for e.g. WorkSites) which is besides the Default Web Site. So my Web API namely OrdersWebAPI I have Integrated Pipeline application pool that the Web API runs off of. I have no problems running and getting the results both in VS2012 environment or just on the browser using for e.g. the URL http://PSSITES:port/OrdersWebAPI/api/Orders
where PSSITES is the host name under which the WorkSites website is created.
2. Now I have an Android App which consumes this WebAPI, for now I want to run on an Emulator before running on the actual device.
3. I created a Galaxy Nexus S emulator GNSEmul1 in my Eclipse environment using the Virtual Device Manager tool.
4. Here's my code for calling the WebAPI using HttpClient
public class MainActivity extends ActionBarActivity
{
public final static String EXTRA_MESSAGE = "com.example.webapimessageapp.MESSAGE";
public static final int main_activity_actions=0x7f0c0002;

@override
protected void onCreate(Bundle savedInstanceState)
{
PackageManager pm = getPackageManager();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
try
{
//---get the package info---
PackageInfo pi =
pm.getPackageInfo("com.example.webapimessageapp", 0);
//---display the versioncode---
TextView tv = (TextView) findViewById(R.id.app_Ver);
tv.setText("VersionCode: " +Integer.toString(pi.versionCode));
}
catch (NameNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
.... some other code

//this is the method that is attached to the button onclick
@SuppressLint("NewApi") public void PassMessageToApiForXML(View view)
{
EditText searchForText = (EditText) findViewById(R.id.search_for);
String searchForString = searchForText.getText().toString();
String apiURL = ""; // "http://PSSites:9810/OrdersWebAPI/api/orders";
String urlString = "";

apiURL = "http://PSSites:9810/OrdersWebAPI/api/orders";
if( (searchForString != null) && (!searchForString.isEmpty()))
{
urlString = apiURL + "/?take=" + searchForString;
new LongProcessAPI().execute(urlString);
}
}
//Now the LongProcessAPI - asynctask
private class LongProcessAPI extends AsyncTask<String, String, String>
{
//Try this way
protected String doInBackground(String... params)
{
String urlString=params[0]; // URL to call
String results = "";
String message = "";
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = null;
HttpResponse response = null;

try
{
request = new HttpGet(urlString);
request.setHeader("Content-Type", "text/xml");
if (request != null)
response = httpClient.execute(request);
}
catch (ClientProtocolException e)
{
e.printStackTrace();
message = e.getMessage();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
message = e.getMessage();
}
.... other code
I run the debug configuration in my Android project within Eclipse where the debug configuration is created for this project and my emulator GNSEmul1 .
On debug:-
The issue is at the - response = httpClient.execute(request); line - this is where I get the Connection refused error. I have tried the following
1. substituting PSSites:9810 with 10.0.2.2 without port
Result in this case the request times out
2. substituting PSSites:9810 with 10.0.2.2:9810
Result - connection refused
I am not sure what I should do. I would appreciate if someone could help in resolving this. I am not the only one facing this issue I gather from reading online.

If need be I can send the code.
 
Last edited:
Back
Top Bottom