简体   繁体   English

Android无法连接网络服务器

[英]Android can't connect webserver

I use "xampp" build a web server,and create "abc.php" on C:\\xampp\\htdocs. 我使用“ xampp”构建Web服务器,并在C:\\ xampp \\ htdocs上创建“ abc.php”。

After finish all work , I link "localhost/abc.php". 完成所有工作后,我链接“ localhost / abc.php”。

use Pc google to cheak the URL can show "This is server's message". 使用Pc google欺骗URL可以显示“这是服务器的消息”。

So far is good.Until I wanna android connect webserver. 到目前为止一切都很好。直到我想用android连接网络服务器。

I try very much way (include close firewall or antivirus etc.) but still fail. 我尝试了很多方法(包括关闭防火墙或防病毒等),但仍然失败。

I have tried google(android phone) to connected " http://MyIP/abc.php ",not through this program. 我已经尝试谷歌(Android手机)连接“ http://MyIP/abc.php ”,而不是通过此程序。

result is can show my "abc.php" in google(android phone). 结果是可以在Google(Android手机)中显示我的“ abc.php”。

but I don't know where error. 但我不知道哪里出错。 I can't use my program get the URL 我无法使用我的程序获取URL

package com.testdb;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.textView1);
        db message = new db();
        String msg = message.stringQuery("http://10.0.2.2/abc.php");
        textView.setText("Server message is "+msg);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

- --

package com.testdb;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class db {

    public String stringQuery(String url){
        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost method = new HttpPost(url);
            HttpResponse response = httpclient.execute(method);
            HttpEntity entity = response.getEntity();
            if(entity != null){
                return EntityUtils.toString(entity);
            }
            else{
                return "No string.";
            }
         }
         catch(Exception e){
             return "Network problem";
         }
    }
}

and this is my log 这是我的日志

04-04 16:30:07.425: W/Trace(1183): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.906: E/Trace(1301): error opening trace file: No such file or directory (2)
04-04 16:30:16.906: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.916: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:16.916: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.006: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.006: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.796: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.796: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.816: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:17.836: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.046: D/libEGL(1301): loaded /system/lib/egl/libEGL_emulation.so
04-04 16:30:18.066: D/(1301): HostConnection::get() New Host Connection established 0x2a0ac400, tid 1301
04-04 16:30:18.106: D/libEGL(1301): loaded /system/lib/egl/libGLESv1_CM_emulation.so
04-04 16:30:18.126: D/libEGL(1301): loaded /system/lib/egl/libGLESv2_emulation.so
04-04 16:30:18.289: W/EGL_emulation(1301): eglSurfaceAttrib not implemented
04-04 16:30:18.309: D/OpenGLRenderer(1301): Enabling debug mode 0
04-04 16:30:18.309: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.316: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.316: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.376: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.386: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.396: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.486: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.496: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.689: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.716: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0
04-04 16:30:18.786: W/Trace(1301): Unexpected value from nativeGetEnabledTags: 0

10.0.2.2 is an IP address on your local network. 10.0.2.2是您本地网络上的IP地址。 It is not accessible from the Internet, so an Android phone with a cellular connection cannot connect to it. 无法从Internet上访问它,因此具有蜂窝连接的Android手机无法连接到它。 You need to run your Web application on a public hosting service, not on your local machine. 您需要在公共托管服务上而不是在本地计算机上运行Web应用程序。

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

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