2015-02-19 13:02
浏览 456


I got this error on an Android Emulator when I tried connecting to a local server:

I/System.out﹕ org.apache.http.conn.HttpHostConnectException: Connection to refused

This is the code that I used to connect to the local server (

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("");
//HttpPost httppost = new HttpPost("");
//HttpPost httppost = new HttpPost("");

try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("username", "Will"));
    nameValuePairs.add(new BasicNameValuePair("password", "password"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
} catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.println("API LOGIN");

Also, I've added <uses-permission android:name="android.permission.INTERNET" /> to the AndroidManifest.xml file. The content of my AndroidManifest.xml is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="" >

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
        <activity android:name=".LoginActivity" android:label="Login" />

        <uses-permission android:name="android.permission.INTERNET" />



There is no problem with the local server, since I can access to the url from the browser of the Android Emulator without problem and also from another computer within network, but the connection is refused from my Android application.

Please help me to figure out what can else I should configure to make it work on my app in the Android Emulator.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douan4359 2015-02-19 13:10

    You'll need to set StrictMode before calling your server:

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
    解决 无用
    打赏 举报

相关推荐 更多相似问题