Android 设备 Web 服务 SYMFONY 3 的连接被拒绝

Connection Refused for Android Device Web Service SYMFONY 3

Connect an Android Device To a Web Service on Local Host

按照我之前的帖子,我现在可以使用 wamp

将我的 Android 设备连接到我的本地主机

但我仍然无法连接到我的 symfony 服务器并获取我的 API 数据

我启动了 symfony 的内部服务器: "Server running on http://127.0.0.1:8000"

我在 AndroidManifest.xml

上使用了互联网许可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

我的MainActivity.java代码

package com.example.cbmedandroid;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        Button b = (Button)findViewById(R.id.my_button);
        b.setClickable(false);
        new LongRunningGetIO().execute();
    }

    private class LongRunningGetIO extends AsyncTask <Void, Void, String> {
        protected String getASCIIContentFromEntity(HttpEntity entity) throws    IllegalStateException, IOException {
            InputStream in = entity.getContent();
            StringBuffer out = new StringBuffer();
            int n = 1;
            while (n>0) {
                byte[] b = new byte[4096];
                n =  in.read(b);
                if (n>0) out.append(new String(b, 0, n));
            }
            return out.toString();
        }

        @Override
        protected String doInBackground(Void... params) {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet("http://192.168.43.13:8000/api/horaire.json");
            String text = null;
            try {
                HttpResponse response = httpClient.execute(httpGet, localContext);
                HttpEntity entity = response.getEntity();
                text = getASCIIContentFromEntity(entity);
            } catch (Exception e) {
                return e.getLocalizedMessage();
            }
            return text;
        }

        protected void onPostExecute(String results) {
            if (results!=null) {
                EditText et = (EditText)findViewById(R.id.my_edit);
                et.setText(results);
            }
            Button b = (Button)findViewById(R.id.my_button);
            b.setClickable(true);
        }
    }
}

当我启动应用程序并单击按钮时。 它在 40 秒内加载,我得到了这个 "Connection to http://192.168.43.13:8000 refused"

192.168.43.13 是我的电脑地址

我应该怎么做才能解决这个问题。 谢谢。

你能安装这个cURL App for Android吗?

然后在你的Android上使用(这是真的phone还是模拟器)打开cURL window然后输入:

cURL http://192.168.43.13:8000/

我用真实的 Android phone 和上面提到的 cURL 应用程序尝试了同样的设置,并放入我的 Symfony web URL(在另一个PC),并且 Android 显示了我期望的正确 html 响应。

至少这将帮助您首先验证功能。

在此行下方编辑:

以下是自 HttpClient 弃用后您可能会使用的代码:

URL url = new URL("http://192.168.43.13:8000/api/horaire.json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//conn.setRequestMethod("GET");

// read the response
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
System.out.println(response);

不确定是否需要使用 "setRequestMethod"。但试试这个改变,看看它是否适合你。

终于!我找到了解决问题的办法

当 运行 内置 php 服务器时。 我们需要指定这个命令

php bin/console server:run 0.0.0.0:8000

(8000是我的端口,你可以放你的)

这样任何设备或主机 (ip) 都可以访问

如果你输入 127.0.0.1 则只允许你的本地主机

这就是为什么我无法获得 Api 即使我通过 wifi 热点连接到我的本地主机

现在可以了