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



我的 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: url 的意外响应代码 500

请告诉我我在这里错过了什么...

编辑 1:

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

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

它重试两次,两次 POST 都成功,但响应是错误 500, 有谁知道如何解决这个问题?我有拉拉维尔后端

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

你试过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'

相关内容

最新更新