0

この問題に関する同様の投稿を見たことがありますが、ASP .NET 用の Visual Studio 開発サーバーの使用に関連するものはありません。

次のエラーが表示されます。

確立された接続が、ホスト マシンのソフトウェアによって中止されました

そして、私は次のコードを実行しています:

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

これを通常の Web ブラウザー (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>
4

2 に答える 2

2

同じ問題に直面しています。ファイアウォールがそれを止めていたので....ファイアウォールをオフにすると、現在動作しています... :)

于 2012-10-31T11:13:43.167 に答える
0

ドキュメントから答えを求めなかったなんて信じられません。127.0.0.1はエミュレーター自体のIPであり、10.0.2.2は開発者マシンのIPであることが明確に示されています。

ローカルホストから10.0.2.2に変更すると、問題が解決しました。

于 2012-05-31T10:33:05.763 に答える