POST com.android.volley.ServerError Laravel后端的意外响应代码500

时间:2018-08-10 17:14:48

标签: android laravel post laravel-5 android-volley

我的api工作正常,但是我收到意外的响应代码500错误 使用以下代码发布到api时:

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Log.i("TAG", "Response: " + response);
                            }

                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, "Error:...", Toast.LENGTH_SHORT).show();
                        error.printStackTrace();
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<>();
                        params.put("name", name);
                        params.put("address", address);
                        params.put("email", email);
                        return params;
                    }
                };
                MySingleton.getmInstance(MainActivity.this).addToRequestQueue(stringRequest);

堆栈跟踪: E / Volley:[258] BasicNetwork.performRequest:网址的意外响应代码500

请告诉我我在这里想念的东西...

编辑1:

先前的错误是由于缺少参数和 按照Anish的建议,我修复了缺少的参数。

但是现在它两次在数据库中发布,并且得到错误响应为:BasicNetwork.performRequest: Unexpected response code 500

它重试两次,两次都成功,但响应为错误500, 有谁知道如何解决这一问题?我有 Laravel后端

2 个答案:

答案 0 :(得分:1)

500错误是内部服务器的错误。请检查所有参数以及请求方法

答案 1 :(得分:0)

您尝试过AndroidNetworking吗?它更易于使用。

例如

首先,您需要像这样将参数收集到JSONObjectJSONArray

 JSONObject params = new JSONObject();

    try {
        params.put("param0", value0);
        params.put("param1", value1);
        params.put("param2", value2);
        params.put("param3", value3);

构造将是这样。 (给出的示例是一个JSONObject)

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()      //<--- this is for the connection time out. if the device response is too long
            .connectTimeout(2, TimeUnit.SECONDS)
            .readTimeout(2, TimeUnit.SECONDS)
            .writeTimeout(2, TimeUnit.SECONDS)
            .build();

    AndroidNetworking.initialize(getContext(), okHttpClient);
    AndroidNetworking.post(baseUrl + "endpoint")
            .addJSONObjectBody(params)
            .addHeaders("Content-Type", "application/json")
            .addHeaders("Prefer", "return=representation")
            .addHeaders("Accept", "application/vnd.pgrst.object+json") //<---- this part is for postgres database. you can remove it if you won't use it
            .setPriority(Priority.HIGH)
            .build().getAsJSONObject(new JSONObjectRequestListener() {

        @Override
        public void onResponse(JSONObject response) {
            Log.e("response EOD", String.valueOf(response));

            try {
                //your Statement here when the server has response

            } catch (JSONException e) {
                e.printStackTrace();
                sendAndgetDispID();
            }
        }

        @Override
        public void onError(ANError anError) {
            try {
                //Log the error if there's any
                if (error.getErrorCode() != 0) {
                    Log.d("onError errorCode", "onError errorCode : " + error.getErrorCode());
                    Log.d("onError errorBody", "onError errorBody : " + error.getErrorBody());
                    Log.d("onError errorDetail", "onError errorDetail : " + error.getErrorDetail());

                } else {
                    Log.d("onError errorDetail", "onError errorDetail : " + error.getErrorDetail());
                }

            } catch (Throwable e) {
                e.printStackTrace();

            }
        });
    } catch(Throwable e){
        e.printStackTrace();
    }

别忘了在您的build.gradle模块应用中声明这一点

implementation 'com.amitshekhar.android:android-networking:1.0.1'