POST 方法的意外响应代码 500

Unexpected response code 500 for POST method

我正在对旧项目进行更新,目前我对 Android 了解不多。在项目中,我们有关于产品的评论部分。

对于之前发送后的评论,我们将 return 设为 0(出现错误)和 1(成功)。

下面是我们使用的代码。

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Method.POST,
    act.getString(R.string.CommentForUserURL),
    null, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(
            JSONObject response) {

        Log.d("response done", "done===" + response);

        mloading.setVisibility(View.GONE);
        if (response != null) {
            Comment obj = new Comment();
            JSONObject jsonObject = response;
            try {
                obj.setComment(jsonObject
                        .getString("Comment"));

现在我们已经将 return 对象从 0/1 更改为用户对象。

这个需要更新JsonObjectRequest为GJSON请求吗?或者对象也将使用 JsonObjectRequest 进行解析?

我问是因为当我在上面执行时,出现如下错误。

01-25 12:30:21.754: E/Volley(16487): [10114] BasicNetwork.performRequest: 
Unexpected response code 500 for 
http://new.souqalharim.com/add/CommentForMerchant

知道我为什么会收到此错误吗?

注意:此 URL 适用于 iPhone 应用程序。


编辑 1

这是post方法,所以完整的url不存在。还有一些参数需要添加,比如 ?comment=MyComment&userId=123&productId=234。因为它是 post 我没有在实际 url.

中添加参数

我有其他方法

@Override
protected Map<String, String> getParams()
        throws AuthFailureError {
    Map<String, String> params = new HashMap<String, String>();
    params.put("productId", productId.toString());
    params.put("userId",
            mSessionManager.getUserCode().toString());
    params.put("comment", GlobalFunctions
            .EncodeParameter(med_comments
                    .getText().toString()));



    return params;
}

完整url如下。

http://new.souqalharim.com/add/CommentForUser?productId=325&userId=5&comment=abcd

我在 Mozilla RESTClient 中对此进行了测试,它工作正常。


编辑 2

进一步检查后我发现 protected Map<String, String> getParams() throws AuthFailureError { 没有被调用。知道为什么会这样吗?

问题如下。

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Method.POST,
    act.getString(R.string.CommentForUserURL),
    null, new Response.Listener<JSONObject>() {
    ^^^^

应该是

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
    Method.POST,
    act.getString(R.string.CommentForUserURL),
    new JSONObject(params), new Response.Listener<JSONObject>() {
    ^^^^^^^^^^^^^^^^^^^^^^

protected Map<String, String> getParams() 复制 final JsonObjectRequest 之前的代码。

就是这样了!!!

原因如下

JsonObjectRequest 被扩展 JsonRequest 直接覆盖 getBody() 方法,所以你的 getParam() 会永远不要调用,我建议你扩展 StringRequest 而不是 JsonObjectRequest.

您可以查看 this 答案了解更多详情。

实际上 500 表示内部服务器错误,这是由您调用的 Rest-api 而不是 Volley 引起的,因此请检查 back-end 代码。