2012-05-25 51 views
0

我已經看到有關此問題的類似帖子,但其中沒有一篇涉及使用Visual Studio開發服務器用於ASP .NET。已建立的連接被您的主機(VS dev服務器)中的軟件中止

我收到以下錯誤消息。

建立的連接被主機中的軟件中止。

而且我執行以下代碼:

String employeesJson = client.downloadString("http://localhost:60000/Api/Employee/GetEmployees.aspx"); 

當我運行這在常規網頁瀏覽器(Chrome瀏覽器21或Internet Explorer 10),它運行得很好。我得到了我想要的JSON結果。

而且我使用的WebClient類(在變量「client」下)定義如下。

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; 

    } 

} 

我AndroidManifest.xml文件看起來是這樣的:

<?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> 

回答

0

我不能相信我沒有尋求從文檔的答案。它清楚地表明,127.0.0.1是模擬器本身的IP,而10.0.2.2是開發者機器的IP。

從localhost更改爲10.0.2.2解決了我的問題。

2

面對同樣的問題,它是防火牆阻止它....所以關閉防火牆,它現在工作... :)

相關問題