Android模拟器与http://10.0.2.2的连接被拒绝 [英] Android Emulator Connection to http://10.0.2.2 refused

查看:252
本文介绍了Android模拟器与http://10.0.2.2的连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试连接到本地服务器时,我在Android模拟器上收到此错误:

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 http://10.0.2.2 refused

这是我用来连接到本地服务器的代码( http://10.0.2.2/bank/login.php ):

This is the code that I used to connect to the local server (http://10.0.2.2/bank/login.php):

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/bank/login.php");
//HttpPost httppost = new HttpPost("http://10.0.2.2:80/bank/login.php");
//HttpPost httppost = new HttpPost("http://10.0.2.2:8080/bank/login.php");

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");
    System.out.println(e);
}

此外,我还向AndroidManifest.xml文件添加了< uses-permission android:name ="android.permission.INTERNET"/> .我的AndroidManifest.xml的内容是:

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="http://schemas.android.com/apk/res/android"
    package="com.dev.will.test" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>
        <activity android:name=".LoginActivity" android:label="Login" />

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

    </application>

</manifest>

本地服务器没有问题,因为我可以访问url http://10.0.2.2/bank/login.php 可以从Android仿真器的浏览器正常运行,也可以从网络内的另一台计算机访问,但是我的Android应用程序拒绝了连接.

There is no problem with the local server, since I can access to the url http://10.0.2.2/bank/login.php 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.

请帮助我弄清楚我还应该配置什么才能使其在Android模拟器中的应用程序上正常工作.

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

推荐答案

在调用服务器之前,您需要设置StrictMode:

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

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

这篇关于Android模拟器与http://10.0.2.2的连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆