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

时间:2016-06-01 20:41:45

标签: android web-services localhost apk symfony

Connect an Android Device To a Web Service on Local Host

按照我之前的帖子,我现在能够使用wamp

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

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

我开始使用symfony的内部服务器: "服务器在http://127.0.0.1:8000"

上运行

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

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);
        }
    }
}

我的MainActivity.java代码

{{1}}

当我启动应用程序并单击按钮时。 它在40秒内加载,我得到了这个 &#34;与http://192.168.43.13:8000的连接被拒绝&#34;

192.168.43.13是我的电脑地址

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

2 个答案:

答案 0 :(得分:2)

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

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

php bin/console server:run 0.0.0.0:8000

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

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

如果您放置127.0.0.1,则只允许您的本地主机

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

现在好了

答案 1 :(得分:1)

您是否可以安装此cURL App for Android

然后在您的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);

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