Android Volley在localhost上使用WCF服务获取错误响应

时间:2015-12-06 18:23:24

标签: android wcf iis android-volley genymotion

我正在尝试使用Volley在Android上使用IIS中托管的WCF RESTful服务。我有下一个代码:

private static final String URL_BASE = "http://10.0.3.2/SimpleRESTServiceCRUD/BookService.svc";
private static final String URL_JSON = "/Books";

List<Book> items;
JsonObjectRequest jsArrayRequest;
private RequestQueue requestQueue;

public BookAdapter(Context context)
{
    super(context,0);

    requestQueue = Volley.newRequestQueue(context);

    JsonObjectRequest jsArrayRequest = new JsonObjectRequest(
            Request.Method.GET,
            URL_BASE + URL_JSON,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    items = parseJson(response);
                    notifyDataSetChanged();
                }
            },
            new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.d(TAG, "Error on JSON response: " + error.getMessage());

                }
            }
    );
}

但是当我调试时总是得到new Response.ErrorListener()但是永远不会进入:

@Override
public void onErrorResponse(VolleyError error) {

    Log.d(TAG, "Error on JSON response: " + error.getMessage());

}

所以我不知道发生了什么!!我正在使用Genymotion Emulator,所以我尝试了这个Uris:

http://192.168.56.1/SimpleRESTServiceCRUD/BookService.svc
http://10.0.3.2/SimpleRESTServiceCRUD/BookService.svc

我甚至使用移动浏览器查看它是否正常工作,我实际上可以使用该服务。

如果有人能帮助我,我将非常感激。 谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的请求尚未发送到WCF服务,因为您没有以下行:

requestQueue.add(jsArrayRequest);

因此,请按以下步骤更新您的代码:

public BookAdapter(Context context)
{
    super(context,0);

    requestQueue = Volley.newRequestQueue(context);

    JsonObjectRequest jsArrayRequest = new JsonObjectRequest(
            Request.Method.GET,
            URL_BASE + URL_JSON,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    items = parseJson(response);
                    notifyDataSetChanged();
                }
            },
            new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.d(TAG, "Error on JSON response: " + error.getMessage());

                }
            }
    );

    requestQueue.add(jsArrayRequest);
}

希望这有帮助!