繁体   English   中英

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

[英]Connection Refused for Android Device Web Service SYMFONY 3

将Android设备连接到本地主机上的Web服务

在我之前的帖子之后,我现在能够使用wamp将我的Android设备连接到我的本地主机

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

我开始使用symfony的内部服务器:“运行在http://127.0.0.1:8000上的服务器”

我在AndroidManifest.xml上使用了Internet权限

<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秒,我得到这个“连接到http://192.168.43.13:8000拒绝”

192.168.43.13是我的电脑地址

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

最后! 我找到了解决问题的方法

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

php bin/console server:run 0.0.0.0:8000

(8000是我的港口,你可以把你的)

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

如果你输入127.0.0.1只允许你的localhost

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

现在好了

你能为Android安装这个cURL应用程序吗?

然后在你的Android上使用(这是一个真正的手机或模拟器)打开一个cURL窗口,然后输入:

cURL http://192.168.43.13:8000/

我尝试使用真正的Android手机和上面显示的cURL应用程序进行同样的设置,并放入我的Symfony网址(在另一台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”。 但尝试这个改变,看看这是否适合你。

暂无
暂无

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

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