问题
I've seen similar posts about this problem, but none of them relate to the use of the Visual Studio development server for ASP .NET.
I'm receiving the following error.
An established connection was aborted by the software in your host machine.
And I'm executing the following code:
String employeesJson = client.downloadString("http://localhost:60000/Api/Employee/GetEmployees.aspx");
When I run this in a regular webbrowser (Chrome 21 or Internet Explorer 10) it runs just fine. I get the JSON result that I want.
And my WebClient
class being used (under the variable "client
") is defined as follows.
public class WebClient {
private HttpClient httpClient;
public WebClient() {
httpClient = new DefaultHttpClient();
}
public String downloadString(String url) throws IOException {
HttpGet get = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(get); //this is where the error occurs.
HttpEntity entity = response.getEntity();
if(entity != null) {
InputStream stream = entity.getContent();
InputStreamReader streamReader = new InputStreamReader(stream);
BufferedReader bufferedReader = new BufferedReader(streamReader);
StringBuilder builder = new StringBuilder();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
builder.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return builder.toString();
}
//catch all the types of exceptions this method can throw. catching "Exception" is considered bad.
} catch (ClientProtocolException e) {
e.printStackTrace();
}
return null;
}
}
My AndroidManifest.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="specialisering.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
回答1:
face same problem, it was firewall stoping it .... so turned off the firewall and it working now... :)
回答2:
I can't believe I didn't seek an answer from the documentation. It clearly states that 127.0.0.1 is the IP of the emulator itself, while 10.0.2.2 is the IP of the developer machine.
Changing from localhost to 10.0.2.2 solved my issue.
来源:https://stackoverflow.com/questions/10757363/an-established-connection-was-aborted-by-the-software-in-your-host-machine-vs-d