简体   繁体   English

无法使用Volley连接到我的本地主机到我的Symfony2 API

[英]Can't connect to my localhost with Volley to my Symfony2 api

I'm actually trying to do a simply GET request using Volley to my Symfony2 api hosted in my pc, first I tried some tutorials getting the JSON from here http://httpbin.org/get?site=code&network=tutsplus and it worked pretty well... Then I started trying to get the JSON from my api but I get some errors and I cant figure why, here is the log: 实际上,我实际上是在尝试使用Volley对PC上托管的Symfony2 api进行简单的GET请求,首先,我尝试了一些教程,可从此处http://httpbin.org/get?site=code&network=tutsplus获取JSON并成功很好...然后我开始尝试从api中获取JSON,但是出现一些错误,我无法弄清楚为什么,这是日志:

       10-07 16:15:54.456 24582-24582/? W/System.err: com.android.volley.NoConnectionError: java.net.ConnectException: failed to connect to /192.168.1.44 (port 80) after 2500ms: isConnected failed: ECONNREFUSED (Connection refused)
10-07 16:15:54.456 24582-24582/? W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
10-07 16:15:54.466 24582-24582/? W/System.err: Caused by: java.net.ConnectException: failed to connect to /192.168.1.44 (port 80) after 2500ms: isConnected failed: ECONNREFUSED (Connection refused)
10-07 16:15:54.466 24582-24582/? W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:223)
10-07 16:15:54.466 24582-24582/? W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
10-07 16:15:54.466 24582-24582/? W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
10-07 16:15:54.466 24582-24582/? W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-07 16:15:54.466 24582-24582/? W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-07 16:15:54.466 24582-24582/? W/System.err:     at java.net.Socket.connect(Socket.java:843)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:152)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:101)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
10-07 16:15:54.466 24582-24582/? W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
10-07 16:15:54.476 24582-24582/? W/System.err:     at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:110)
10-07 16:15:54.476 24582-24582/? W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
10-07 16:15:54.476 24582-24582/? W/System.err:  ... 1 more
10-07 16:15:54.476 24582-24582/? W/System.err: Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
10-07 16:15:54.476 24582-24582/? W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:208)

The request is as simple as: 该请求很简单:

@Override protected void onCreate(Bundle savedInstanceState) { @Override受保护的void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
String url = "http://192.168.1.44/api/fincas/1";

final JsonObjectRequest jsonRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // the response is already constructed as a JSONObject!
                System.out.println(response.toString());
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

Volley.newRequestQueue(this).add(jsonRequest);
setContentView(R.layout.activity_main);

My AndroidManifest : 我的AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alvaro.pruebarequests">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>
    </application>

</manifest>

And the JSON it should get is [{"id":1,"irrigation_type":"type","plant_variety":"variety","hand_number":722,"born_register":"2005-05-05T00:00:00+0000"}] 它应该获取的JSON是[{"id":1,"irrigation_type":"type","plant_variety":"variety","hand_number":722,"born_register":"2005-05-05T00:00:00+0000"}]

Im very frustated with this, since I couldn't fix it... 我对此感到非常沮丧,因为我无法修复它...

How does the webserver run? 网络服务器如何运行?

With embed php server, nginx, apache, other web server? 与嵌入php服务器,nginx,apache或其他网络服务器? (what is the vhost conf) (什么是vhost conf)

好吧,我想出了解决方案,我没有标题“ Accept json”,如果有人遇到这个问题,这是解决方法:)只需添加标题

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

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