简体   繁体   中英

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:

       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) {

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 :

<?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"}]

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? (what is the vhost conf)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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