POST方法的意外响应代码500

use*_*156 12 android json android-volley

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

为了在发送之前发表评论,我们返回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"));
Run Code Online (Sandbox Code Playgroud)

现在我们将返回对象从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
Run Code Online (Sandbox Code Playgroud)

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

注意:此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;
}
Run Code Online (Sandbox Code Playgroud)

完整网址如下.

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

我在Mozilla RESTClient中测试了它,它工作正常.


编辑2

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

Fah*_*kar 9

问题如下.

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

它应该是

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

protected Map<String, String> getParams()以前复制代码final JsonObjectRequest.

而已!!!

原因如下.

JsonObjectRequest扩展JsonRequest其覆盖getBody()方法直接,让你的getParam()不只永远不会调用,我建议您扩展StringRequest而不是JsonObjectRequest.

您可以查看答案以获取更多详细信息.


小智 6

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