Java 当我尝试向JSONArrayRequest注册时
我收到以下消息com.android.volley.ParseError:org.json.JSONException:Value类型为java.lang.String的电子邮件无法转换为JSONObjectError 我没有得到任何回应 JsonObjectRequest jsr=新的JsonObjectRequest(Request.Method.POST,REGISTER_URL,params,new Response.Listener(){Java 当我尝试向JSONArrayRequest注册时,java,android,Java,Android,我收到以下消息com.android.volley.ParseError:org.json.JSONException:Value类型为java.lang.String的电子邮件无法转换为JSONObjectError 我没有得到任何回应 JsonObjectRequest jsr=新的JsonObjectRequest(Request.Method.POST,REGISTER_URL,params,new Response.Listener(){ @覆盖 公共void onResponse(J
@覆盖
公共void onResponse(JSONObject响应){
Log.d(标记,response.toString()+“response”);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.d(标记,错误+“错误”);
if(error instanceof ParseError){
}
}
})
{
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
返回标题;
}
};
/**
*设置注册屏幕的持续时间,并将JsonObjectRequest放入volley RequestQueue类中。
*并根据RequestQueue类执行操作。
*/
jsr.setRetryPolicy(新的DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT\u最大重试次数,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue RequestQueue=Volley.newRequestQueue(Registers.this);
add(jsr);
打破
}
}
当我尝试使用StringRequest注册时,我在后端收到此消息
警告10024---[nio-6060-exec-5].w.s.m.s.DefaultHandlerExceptionResolver:无法读取HTTP消息:org.springframework.HTTP.converter.httpMessageNodeAbleException:无法读取JSON文档:无法识别的令牌“phoneno”:应为(“true”、“false”或“null”)
StringRequest sr=new StringRequest(Request.Method.POST,REGISTER\u URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.d(TAG,response.toString());
JSONObject参数=新的JSONObject();
试一试{
参数put(“名称”,最终名称);
参数put(“电子邮件”,finalMail);
参数put(“城市”,最终);
参数put(“密码”,finalPASSWORD);
参数put(“phoneno”,finalMobile);
}捕获(例外情况除外){
例如printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.d(标记,error.toString());
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“城市”,最终);
参数put(“电子邮件”,finalMail);
参数put(“名称”,最终名称);
参数put(“密码”,finalPASSWORD);
参数put(“phoneno”,finalMobile);
返回参数;
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
*//*参数put(“名称”,最终名称1);
参数put(“电子邮件”,finalMail1);
参数put(“城市”,最终);
参数put(“密码”,finalPASSWORD);
参数put(“phoneno”,finalMobile);
*//*参数put(“内容类型”,“应用程序/json;字符集=utf-8”);
返回参数;
}
};
高级setRetryPolicy(新的DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT\u最大重试次数,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue RequestQueue=Volley.newRequestQueue(Registers.this);
requestQueue.add(sr);
打破
}}
你能告诉我为什么要在onResponse上创建JsonObject吗?我正试图在onResponse上创建字符串,但遇到语法错误。你在执行一些错误的实现。您将在onResponse获得API响应,然后您应该根据需求解析响应,然后根据响应创建新的Json。我不明白当我使用JSONObjectrequest时,我从服务器com.android.volley.ParseError获得响应:org.Json.jsoneException:无法将java.lang.String类型的值电子邮件转换为JSONObjectError,但我在后端看到它将注册,然后我不知道如何处理它。您能发送您的响应以便我可以在这方面提供帮助吗?
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString() + "Response");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error + "Error");
if( error instanceof ParseError) {
}
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
/**
* Set Time duration for Register Screen and and put JsonObjectRequest in volley RequestQueue Class.
* and take action according to RequestQueue Class.
*/
jsr.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(Registers.this);
requestQueue.add(jsr);
break;
}
}
StringRequest sr = new StringRequest(Request.Method.POST, REGISTER_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG,response.toString());
JSONObject params = new JSONObject();
try {
params.put("name", finalNAME);
params.put("email", finalMail);
params.put("city", finalCITY);
params.put("password", finalPASSWORD);
params.put("phoneno", finalMobile);
} catch (Exception ex) {
ex.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG,error.toString());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("city", finalCITY);
params.put("email", finalMail);
params.put("name", finalNAME);
params.put("password", finalPASSWORD);
params.put("phoneno", finalMobile);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
*//* params.put("name", finalNAME1);
params.put("email", finalMail1);
params.put("city", finalCITY);
params.put("password", finalPASSWORD);
params.put("phoneno", finalMobile);
*//* params.put("Content-Type", "application/json; charset=utf-8");
return params;
}
};
sr.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(Registers.this);
requestQueue.add(sr);
break;
}}