意外的响应code 500的POST方法 [英] Unexpected response code 500 for POST method
问题描述
我做的老项目和放大器的最新情况;我没有安卓的多的知识,现在。在项目中,我们对产品的注释部分。
I am doing an update on the old project & I don't have much knowledge of Android as of now. In project we have Comments section on product.
有关早期发送后评论我们已经恢复为0(某些错误)及1(成功)。
For comment after sending earlier we had return as 0 (some error) & 1 (success).
下面是我们使用了code。
Below is the code we were using.
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到用户对象。
Now we have changed the return object from 0/1 to user object.
这是否需要需要更新JsonObjectRequest到GJSON要求?或者,对象也将被解析与JsonObjectRequest?
Does this need need to update JsonObjectRequest to GJSON request? Or object will also get parsed with JsonObjectRequest?
我问,因为当我执行上面的,我得到如下错误。
I am asking because when I execute above, I get error as below.
01-25 12:30:21.754: E/Volley(16487): [10114] BasicNetwork.performRequest:
Unexpected response code 500 for
http://new.souqalharim.com/add/CommentForMerchant
任何想法,为什么我收到此错误?
Any idea why I am getting this error?
注:这个URL工作正常的iPhone应用程序的
这是POST方法,所以完整的URL是不存在的。很少有多个参数添加赞评论= MyComment和放大器;用户id = 123&安培; productId参数= 234。由于这是后我没有在实际的URL添加参数。
This is post method, so full url is not there. There are few more parameters to add like ?comment=MyComment&userId=123&productId=234. As it is post I am not adding parameters in actual url.
我有那些在另外一个方法
I have those in another methods
@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是如下。
Full url is as below.
的http://new.souqalharim.com/add/CommentForUser?productId=325&userId=5&comment=abcd
我在Mozilla RESTClient测试这一点,它工作正常。
I tested this in Mozilla RESTClient and it works fine.
进一步检查后,我发现保护地图&LT;字符串,字符串&GT; getParams()方法抛出AuthFailureError {
是没有得到调用。任何想法,为什么会这样?
After checking further I found protected Map<String, String> getParams() throws AuthFailureError {
is not getting called. Any idea why this is happening?
推荐答案
现在的问题是如下:
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>() {
^^^^^^^^^^^^^^^^^^^^^^
从 复制code保护地图&LT;字符串,字符串&GT; getParams()方法
在最后JsonObjectRequest
。
这就是它!
原因如下。
在 JsonObjectRequest 延长<一个href="https://android.googlesource.com/platform/frameworks/volley/+/master/src/com/android/volley/toolbox/JsonRequest.java"相对=nofollow> JsonRequest 其覆盖 getBody()
方法直接,让您的 getParam()永远不会调用,我建议您延长 StringRequest 而不是JsonObjectRequest。
The JsonObjectRequest is extended JsonRequest which override getBody()
method directly, so your getParam() would never invoke, I recommend you extend StringRequest instead of JsonObjectRequest.
您可以检查这回答的更多细节。
your can check this answer for more details.
这篇关于意外的响应code 500的POST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!