我在android服务中使用JsonArrayRequest:
public int onStartCommand(Intent intent, int flags, final int startId) {
//...
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(Request.Method.GET, url, "", new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.i("SUCCESS", response.toString());
stopSelf(startId);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
stopSelf(startId);
}
});
CORE.getInstance(getApplicationContext()).addToRequestQueue(jsonArrayRequest);
Log.i("StationsService", "onStartCommand END");
return START_STICKY;
}
运行后,我在stopSelf(startId)上获得了Exception;:
java.lang.IllegalMonitorStateException: object not locked by thread before wait()
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:401)
at com.omsk.wwtlf.istanbultransportnet.api.services.StationsService$1.onResponse(StationsService.java:42)
at com.omsk.wwtlf.istanbultransportnet.api.services.StationsService$1.onResponse(StationsService.java:36)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
你能否建议一个正确的方法来停止Volley的服务?
答案 0 :(得分:0)
传递jsonarray作为请求而不是&#34;&#34; (空白字符串)
public int onStartCommand(Intent intent, int flags, final int startId) {
//...
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(Request.Method.GET, url, <jsonarray-object>, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.i("SUCCESS", response.toString());
stopSelf(startId);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
stopSelf(startId);
}
});
CORE.getInstance(getApplicationContext()).addToRequestQueue(jsonArrayRequest);
Log.i("StationsService", "onStartCommand END");
return START_STICKY;
}
答案 1 :(得分:0)
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("name", "value");
return params;
}
then make new constructor :
public PostJsonArrayRequest(String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
super(Method.POST, url, null, listener, errorListener);
}