Android Volley的返回响应

时间:2018-08-24 16:35:23

标签: android function http android-volley

我编写了一个函数,该函数发出HTTP请求,并将响应存储在Bundle中以随后初始化活动。

public static void communicate(final Context context, String url, final String typeResponse, final Intent intent) {
    RequestQueue queue = Volley.newRequestQueue(context);

    RequestFuture<String> future = RequestFuture.newFuture();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, BASE_URL + url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Toast.makeText(context, response, Toast.LENGTH_SHORT).show();
            Bundle bundle = new Bundle();
            switch (typeResponse) {
                case "text":
                    bundle.putString("response", response);
                    break;
                case "json":
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray names = jsonObject.names();
                        for (int i = 0; i < names.length(); i++) {
                            //Toast.makeText(context, names.getString(i), Toast.LENGTH_SHORT).show();
                            bundle.putString(names.getString(i), jsonObject.getString(names.getString(i)));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            intent.putExtras(bundle);
            context.startActivity(intent);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("test", "hi!!");
            return params;
        }
    };
    queue.add(stringRequest);
}

但是我想返回Bundle对象以使用该函数,如下所示:

Bundle myBundle = communicate('httl://qwe.asd', 'json')

如何修改我的功能?

谢谢。

1 个答案:

答案 0 :(得分:0)

排球请求是异步的,因此我建议您将onResponse其他函数放在内部以处理捆绑包。 同样,您可以创建一个接口以在其他位置发送响应。像这样

界面

 public interface onResponseCallback {
  void onResponse(Bundle bundle);
}

活动

      public MyActivity extends AppCompatActivity implements onResponseCallback{


        public void onCreate(Bundle....){
    MyRequest myrequest = new MyRequest(this);
        ..}

        public void onResponse(Bundle bundle){
        //bundle argument is your response from request,
        // do some with your response
Intent intent = new Intent....
intent.putExtras(bundle);
            startActivity(intent);
        }


        }

请求类

public class MyRequest{

OnResponseCallback onResponseCallback= null;

public MyRequest(onResponseCallback onResponseCallback)
this.onResponseCallback = onResponseCallback;
}

public void communicate(final Context context, String url, final String typeResponse, final Intent intent) {
    RequestQueue queue = Volley.newRequestQueue(context);

    RequestFuture<String> future = RequestFuture.newFuture();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, BASE_URL + url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Toast.makeText(context, response, Toast.LENGTH_SHORT).show();
            Bundle bundle = new Bundle();
            switch (typeResponse) {
                case "text":
                    bundle.putString("response", response);
                    break;
                case "json":
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray names = jsonObject.names();
                        for (int i = 0; i < names.length(); i++) {
                            //Toast.makeText(context, names.getString(i), Toast.LENGTH_SHORT).show();
                            bundle.putString(names.getString(i), jsonObject.getString(names.getString(i)));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
             onResponseCallback.onResponse(bundle);   
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("test", "hi!!");
            return params;
        }
    };
    queue.add(stringRequest);
}

}

如果您不喜欢这些,也许您可​​以使用常量或放入共享的首选项来保存您的bundle对象。

希望对您有所帮助。