【问题标题】:How can I make my code a synchronous POST volley request?如何使我的代码成为同步 POST volley 请求?
【发布时间】:2020-11-05 23:39:06
【问题描述】:

我正在构建一个用于锻炼的跟踪应用程序。随着 gps 每 1 秒更新一次,纬度和经度被添加到一个数组中。在练习结束时,当您按下保存时,将执行以下方法,将所有坐标发送到数据库。因为它是一个异步请求,所以坐标不会以正确的顺序加载到数据库中。我该如何解决这个问题,所以它会等到循环的每次迭代完成或类似的事情。谢谢

/* 从 latitudeAndLongitudePoints ArrayList 中插入经纬度点 进入 db*/

中的 latitudeandlongitudepoints 表

private void insertLatitudeAndLongitudePoints(){

    //iterates though array of co-ordinates, and adds to database
    for(int loop=0; loop<latitudeAndLongitudePoints.size();loop++) {

        final int index = loop;

        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                "http://rrush01.lampt.eeecs.qub.ac.uk/insertLatitudeAndLongitudePoints.php",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();

                params.put("Latitude", String.valueOf(latitudeAndLongitudePoints.get(index).latitude));
                params.put("Longitude", String.valueOf(latitudeAndLongitudePoints.get(index).longitude));
                params.put("User", AccountInfo.accountEmail);

                return params;
            }
        };

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringRequest);
    }
}

【问题讨论】:

    标签: java android android-studio http-post android-volley


    【解决方案1】:

    在 build.gradle 中添加 Volley 库的依赖项。

    compile 'com.android.volley:volley:1.0.0'
    

    Volley 是一个网络库,它可以让 Android 应用程序的 HTTP 请求更快速、更容易。使用 Volley 库,我们不需要创建 AsyncTask 来在 volley 中进行多个 API 调用,我们可以使用 RequestQueue 将每个请求排入队列。在此示例中,我们使用了 Google Place 自动完成 API。我们将使用 volley 进行同步 HTTP 请求。要进行同步 HTTP 请求,请创建一个 RequestFuture 对象。并让JsonObjectRequest传递这个参数RequestMethod,URL,传递Json类型的对象,传递RequestFuture实例。

        RequestFuture<JSONObject> requestFuture=RequestFuture.newFuture();
        final String mURL = "https://maps.googleapis.com/maps/api/place/
        autocomplete/json?key="+KEY+"&input=" + input;
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
        mURL,new JSONObject(),requestFuture,requestFuture);
        MySingleton.getInstance(mContext).addToRequestQueue(request);
    

    在 RequestQueue 中添加您的请求。现在,每当用户发出 HTTP 请求时,每个请求都会添加到 RequestQueue。为了从 requestFuture 获取响应对象,我们调用 get() 方法。我们还设置了 10 秒的超时时间,这样我们就不会无限期地阻塞 UI 线程,以防我们的请求超时。

    try {
            JSONObject object= requestFuture.get(10,TimeUnit.SECONDS);
        } catch (InterruptedException e|ExecutionException e|TimeoutException e) {
            e.printStackTrace();
        }
    

    希望这有助于您了解我们如何使用 volley 来发出同步 HTTP 请求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 2021-03-11
      • 1970-01-01
      • 2019-01-26
      • 2021-10-15
      相关资源
      最近更新 更多