我正在使用 Volley 作为我的应用程序的网络处理程序。我的后端是 Apache 服务器上的 Laravel 5。我正在尝试按照此处所述使用自定义请求进行 POST:Volley JsonObjectRequest Post request not working ,但它会保持错误并返回 500 代码错误响应。还有其他解决方案吗?还是我错过了什么?我不熟悉 Laravel。谢谢。
这是我的代码:
public class CustomRequest extends Request<JSONObject> {
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
Log.d("parameter", params.toString());
return params;
};
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
//headers.put("X-CSRF-Token", params.get("_token"));
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
//Log.d("token",params.get("_token"));
return headers;
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
关于主要 Activity :
final String POST_URL = "https://www.pinus.com/api/registertoserver";
final HashMap<String, String> postParams = new HashMap<String, String>();
postParams.put("email", email);
postParams.put("first_name", firstName);
postParams.put("last_name", lastName);
postParams.put("alamat", adress);
postParams.put("notelp", phone);
final RequestQueue queue = Volley.newRequestQueue(getApplicationContext(), new OkHttpStack());
CustomRequest jsonObjReq = new CustomRequest(Request.Method.POST, POST_URL, postParams, new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("result", response.toString());
if (response.getBoolean("status")) {
//pDialog.dismiss();
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// Adding request to request queue
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObjReq);
queue.start();
最佳答案
我知道你 6 个月前问过,但我会回答,也许它对其他人有用。我有同样的问题,问题是 CSRF token 。在文件中:App\Http\Middleware\VerifyCsrfTokenMiddleware.php
,找到这个
protected $except = [
];
并为您的 API 添加路由。
关于php - Android:使用 volley 将数据发布到 laravel 服务器时出现意外响应代码 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33317510/