dqhdz04240 2016-10-07 15:27 采纳率: 0%
浏览 66
已采纳

无法使用Volley连接到我的本地主机到我的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...

  • 写回答

2条回答 默认 最新

  • duanci1939 2016-11-07 17:17
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序