首页 > 解决方案 > 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')

如何修改我的功能?

谢谢。

标签: androidfunctionhttpandroid-volley

解决方案


Volley 请求是异步的,因此我建议您将 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);
}

}

如果你不喜欢这个,也许你可以使用常量或放入 sharedpreferences 来保存你的包对象。

我希望这对你有帮助。


推荐阅读