简体   繁体   English

通过android连接localhost - 连接到10.0.2.2被拒绝

[英]Connecting localhost via android - Connection to 10.0.2.2 refused

I have an apache server installed in my PC where I host one PHP file. 我在我的PC上安装了一个apache服务器,我在其中托管一个PHP文件。

Then I tried to connect to that file using eclipse, but it always gives me below error 然后我尝试使用eclipse连接到该文件,但它总是给我以下错误

connection to http://10.0.2.2:8080 refused.

I tried changing address to the followings, but got similar error all the time. 我尝试将地址更改为以下内容,但始终出现类似错误。

http://10.0.2.2
http://127.0.0.1
http://localhost

Can anyone please help me. 谁能帮帮我吗。

EDITED: Just for information, I can access to remoter server (eg www.mydomain.com) without any problem. 编辑:仅供参考,我可以毫无问题地访问远程服务器(例如www.mydomain.com)。

CLASS FILE: 课程文件:

HttpClient httpclient = new DefaultHttpClient();
Log.d("test","t0");
HttpPost httppost = new HttpPost("http://10.0.2.2:8080/mylibman/data.php");
Log.d("test","t1");
HttpResponse response = httpclient.execute(httppost); // error here
Log.d("test","t2");

Android Manifest: Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.migrationdesk.mylibman"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET" />

Error LogCat: 错误LogCat:

11-16 02:02:20.803: E/log_tag(1427): Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
11-16 02:02:20.803: E/log_tag(1427): Error converting result java.lang.NullPointerException: lock == null
11-16 02:02:20.803: E/log_tag(1427): Error parsing data org.json.JSONException: End of input at character 0 of 

alright go to Apache httpd.conf (located in Apache folder): and search for 好的,转到Apache httpd.conf(位于Apache文件夹中):并搜索

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

and check if the second line is Deny, if it is then change it to: 并检查第二行是否为Deny,如果是,则将其更改为:

Allow from all

then restart the Appache server, and tell me the feadback. 然后重启Appache服务器,并告诉我feadback。

Edit 编辑

try it out in your real device: 在您的真实设备中尝试:

go to CMD and type ipconfig under IPv4 take the IP address and change the IP it will look similar to this: 转到CMD并在IPv4下键入ipconfig取IP地址并更改IP,它看起来类似于:

http://192.168.0.106:8080/mylibman/data.php // similar to this.

Turn Off the firewall and any anti-virus application in your PC 关闭PC中的防火墙和任何防病毒应用程序

and please give me the feedback. 请给我反馈。

Mike helped me a lot on this issue. 迈克在这个问题上给了我很多帮助。 So here my steps to avoid this Connection refused . 所以这里我的步骤避免这种连接被拒绝

1) Add if to test Android SDK. 1)添加if测试Android SDK。 It seems that in new versions you have to explicity allow the connection to be open in a different thread: 似乎在新版本中你必须明确允许连接在不同的线程中打开:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            System.out.println("*** My thread is now configured to allow connection");
        }

... ...

2) Add an exception for your port: 2)为您的端口添加例外:

in your case allow inbound connection for port 8080 在您的情况下允许端口8080入站连接

3) test your connection by browsing the desired url locally 3)通过在本地浏览所需的URL来测试您的连接

use your browser and try to visit http://10.0.2.2:8080 locally 使用您的浏览器并尝试在本地访问http://10.0.2.2:8080

4) Add in your manifest file a permission for internet: 4)在您的清单文件中添加一个Internet权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourapp"
    android:versionCode="1"
    android:versionName="1.1" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

I am using Wamp, and its apache is listening to servername localhost and port 8080 (allow from all in httpd.conf). 我正在使用Wamp,它的apache正在侦听servername localhost和端口8080(允许来自httpd.conf中的所有内容)。

My local IP is something similar to 192.168.0.XXX/folder/my-page-here 我的本地IP类似于192.168.0.XXX/folder/my-page-here

You can use your real IP address to reach your virtual server. 您可以使用真实IP地址访问虚拟服务器。 Take a look at my answer here . 看看我的答案在这里 I hope it helps. 我希望它有所帮助。

if your SDK version is less then 9 then add above code for SDK version them some changes in Apache update httpd.conf as line deny from all..replace that sentence Allow from all and restart services of WAMP server then use 10.0.2.2 address or 10.0.2.2:8080 port no and include permission line in android AndroidManifest file 如果您的SDK版本低于9,那么为SDK版本添加上面的代码,他们在Apache更新httpd.conf中的一些更改作为来自所有的行拒绝。替换句子允许来自所有并重新启动WAMP服务器的服务然后使用10.0.2.2地址或10.0.2.2:8080端口号并在Android AndroidManifest文件中包含权限行

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

我通过在url中写入cmd -> ipconfig IP地址来解决这个问题:

 HttpPost httppost = new HttpPost( "http://192.168.x.x/mylibman/data.php" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM