Android Volley 在本地主机上使用 WCF 服务获取错误响应
Android Volley getting Error Response using WCF Service on localhost
我正在尝试使用 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 模拟器,所以我尝试了这个 Uris:
http://192.168.56.1/SimpleRESTServiceCRUD/BookService.svc
http://10.0.3.2/SimpleRESTServiceCRUD/BookService.svc
我什至使用移动浏览器查看它是否正常工作,我实际上可以访问该服务。
如果有人能帮助我,我将不胜感激。
谢谢!!
我认为您的请求尚未发送到 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);
}
希望对您有所帮助!
我正在尝试使用 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 模拟器,所以我尝试了这个 Uris:
http://192.168.56.1/SimpleRESTServiceCRUD/BookService.svc
http://10.0.3.2/SimpleRESTServiceCRUD/BookService.svc
我什至使用移动浏览器查看它是否正常工作,我实际上可以访问该服务。
如果有人能帮助我,我将不胜感激。 谢谢!!
我认为您的请求尚未发送到 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);
}
希望对您有所帮助!