Alvaro Falcon Punch Alvaro Falcon Punch - 25 days ago 17
PHP Question

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...

Answer

Well I Figured out the solution, I was missing the header "Accept json", if somebody is having this problem, here is how to fix it :) just add that header

Comments