0

实际上,我在这里发布了太多关于连接网站的问题。我很抱歉,伙计们。

这是我的 Json.java

public class Json extends Activity{

    TextView tvStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.json);  
        tvStatus = (TextView) findViewById(R.id.tvStatus);
        new showThat().execute("http://localhost");

    }

    public String readLocalhost() throws ClientProtocolException, IOException
    {
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://localhost");
        HttpResponse response = client.execute(get);
        StatusLine status = response.getStatusLine();
        String data = status.toString();    

        return data;
    }

    public class showThat extends AsyncTask<String, String, String>{


        protected void onPreExecute(String f) {
            // TODO Auto-generated method stub
            f = "saffet";
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                String data = readLocalhost();
                return data;
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String result) {
            tvStatus.setText(result);
        }

    }



}

我只是想打印状态。这里还有logcat!

10-23 14:13:53.276: D/gralloc_goldfish(1429): Emulator without GPU emulation detected.
10-23 14:13:54.057: W/System.err(1429): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
10-23 14:13:54.076: W/System.err(1429):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
10-23 14:13:54.076: W/System.err(1429):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-23 14:13:54.086: W/System.err(1429):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-23 14:13:54.086: W/System.err(1429):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-23 14:13:54.086: W/System.err(1429):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-23 14:13:54.086: W/System.err(1429):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-23 14:13:54.096: W/System.err(1429):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-23 14:13:54.096: W/System.err(1429):     at com.example.channel.Json.readLocalhost(Json.java:36)
10-23 14:13:54.096: W/System.err(1429):     at com.example.channel.Json$showThat.doInBackground(Json.java:55)
10-23 14:13:54.096: W/System.err(1429):     at com.example.channel.Json$showThat.doInBackground(Json.java:1)
10-23 14:13:54.096: W/System.err(1429):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-23 14:13:54.106: W/System.err(1429):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-23 14:13:54.106: W/System.err(1429):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-23 14:13:54.106: W/System.err(1429):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-23 14:13:54.106: W/System.err(1429):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-23 14:13:54.116: W/System.err(1429):     at java.lang.Thread.run(Thread.java:841)
10-23 14:13:54.116: W/System.err(1429): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
10-23 14:13:54.146: W/System.err(1429):     at libcore.io.IoBridge.connect(IoBridge.java:114)
10-23 14:13:54.146: W/System.err(1429):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-23 14:13:54.146: W/System.err(1429):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-23 14:13:54.146: W/System.err(1429):     at java.net.Socket.connect(Socket.java:842)
10-23 14:13:54.156: W/System.err(1429):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-23 14:13:54.156: W/System.err(1429):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-23 14:13:54.156: W/System.err(1429):     ... 15 more
10-23 14:13:54.166: W/System.err(1429): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
10-23 14:13:54.206: D/dalvikvm(1429): GC_FOR_ALLOC freed 134K, 8% free 2886K/3136K, paused 43ms, total 44ms
10-23 14:13:54.216: W/System.err(1429):     at libcore.io.Posix.connect(Native Method)
10-23 14:13:54.216: W/System.err(1429):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
10-23 14:13:54.216: W/System.err(1429):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-23 14:13:54.216: W/System.err(1429):     at libcore.io.IoBridge.connect(IoBridge.java:112)
10-23 14:13:54.216: W/System.err(1429):     ... 20 more

不幸的是,我没有得到停止的错误。但我无法打印状态。

编辑:我使用 Xampp。

4

2 回答 2

3

如果您尝试连接到http://localhost您正在尝试连接到 Android 设备内的服务器,所以我想您想要的是连接到您计算机中运行的服务器。然后您要做的就是连接到您的计算机本地 IP,因此如果您的计算机本地 IP 是 192.168.1.1,那么您为连接提供的 URL 将是http://192.168.1.1

还要记住你的服务器正在监听的端口,通常在本地运行的服务器的默认配置是 8080 或 5000。所以,如果你的服务器正在监听端口 8080 并且你的计算机 IP 是 192.168.1.1,那么你应该使用的 URL提供将是http://192.168.1.1:8080

于 2013-10-23T18:29:51.010 回答
1

您无法从您的 android 设备(模拟器)访问 localhost,因为您的设备上没有 localhost。您必须使用 PC 的真实 IP 地址连接到您的网络服务器。例如,您的 IP 地址是 192.168.127.1,您必须拨打

new showThat().execute("http://192.168.127.1");

编辑:看看这个线程

干杯

于 2013-10-23T18:29:22.170 回答