将JSON从自定义VolleyCallback接口返回到调用方法

时间:2016-01-12 08:23:48

标签: android json android-volley

我有以下方法:它的作用是从另一个活动调用userSignIn方法并返回一个JsonObject,因为我的volley实现以异步方式调用它我已经实现了回调接口,该接口从 OnResponce获取结果方法。然而,面临的困难是将JSONObject返回给调用Activity。

if(mail($destinataire, $sujet, $message, $entete))
{
  //sent
}else{
  //not sent
}

我也试过这个以返回JsonObject但它不能正常工作

@Override
public JSONObject userSignIn(String user, String pass,String version, String authType, String URL) throws Exception {

    urlBuilder = new StringBuilder(URL);
    urlBuilder.append("?uname=" + user);
    urlBuilder.append("&passwd=" + pass);
    urlBuilder.append("&ver=" + version);

    URI=urlBuilder.toString();


     VolleyLoginExecute(new VolleyCallback() {
         @Override
         public JSONObject onSuccess(JSONObject result) {
             responce = result;
             return responce;

         }


     });


    return responce;
}
public interface VolleyCallback{
    JSONObject onSuccess(JSONObject result);

}

public JSONObject VolleyLoginExecute(final VolleyCallback callback) {

    JsonObjectRequest LoginReq = new JsonObjectRequest(Request.Method.GET,URI, null,new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {

            if (response != null) {
                callback.onSuccess(response);

            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    })
    {
        @Override
        public Priority getPriority() {
            return priority;
        }
    };
    LoginReq.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    Acme_Application.getInstance().addToRequestQueue(LoginReq, tag_login_req);

    return responce;
}

如何将JsonObject成功返回到调用活动

1 个答案:

答案 0 :(得分:2)

android.os.Process.killProcess(android.os.Process.myPid());