VOLLEY / ANDROID:JsonArrayRequest的错误响应

时间:2015-08-17 09:03:45

标签: android json http laravel android-volley

我在Android上使用Volley库,我一直都有TimeoutError响应。

我想将JSONArray发送到我的Apache服务器。 请求之后,我收到了jsonArray,我可以在SqLite数据库中存储它,但我的Android日志中总是出错。

错误:D / MyService(5707):onErrorResponsecom.android.volley.TimeoutError

我的安卓代码:

            JSONArray jsonRequest = new JSONArray();
        for(MyLocation myLocation : ListLocation){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("serial", myLocation.serial);
                jsonObject.put("longitude", myLocation.longitude);
                jsonObject.put("latitude", myLocation.latitude);
                jsonObject.put("altitude", myLocation.altitude);
                jsonObject.put("accuracy", myLocation.accuracy);
                jsonObject.put("detect_at", myLocation.date);
                jsonRequest.put(jsonObject);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        JsonArrayRequest stringRequest = new JsonArrayRequest(Method.POST,URL, jsonRequest, 
                    new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, "dropTable");
                Log.d(TAG, "response " + response );
                dabAcces.dropTable();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "onErrorResponse" + error);
                if (row > MAX_REGISTER_GPS_DATA) {
                    Log.d(TAG, "deleteOldestRecord");
                    dabAcces.deleteOldestRecord();
                }
            }

        });
        // Add the request to the RequestQueue.
        queue.add(stringRequest);
    }

我的Laravel / PHP代码:

    public function store()
{

    foreach ($input as $values){
            $tracker = new Tracker;
            $tracker->serial = $values["serial"];
            $tracker->latitude  = $values["latitude"];
            $tracker->longitude = $values["longitude"];
            $tracker->altitude  = $values["altitude"];
            $tracker->accuracy  = $values["accuracy"];
            $tracker->detect_at = $values["detect_at"];
            $tracker->created_at = \Carbon\Carbon::now()->toDateTimeString();
            $tracker->updated_at = \Carbon\Carbon::now()->toDateTimeString();
            $tracker->save();

        }
}

如果我添加

            stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS*5, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

我现在有这个错误:

onErrorResponsecom.android.volley.ParseError: org.json.JSONException: Value <link of type java.lang.String cannot be converted to JSONArray

1 个答案:

答案 0 :(得分:1)

通过添加以下行来更改默认TimeOut。 queue.add(stringRequest);

stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS*5, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));