我正在对旧项目进行更新,到目前为止,我对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"));
现在,我们将返回对象从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。由于是后期发布,因此我不会在实际的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;
}
完整网址如下。
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答案以获取更多详细信息。