如何在android中使用volley向服务器发送json对象
我想使用POST方法将JSONObject发送到服务器。我使用了volley库来传递字符串params,它工作正常,但是如果我尝试使用json对象,在这里调用json对象时显示错误就是我的代码如何在android中使用volley向服务器发送json对象,android,json,android-volley,Android,Json,Android Volley,我想使用POST方法将JSONObject发送到服务器。我使用了volley库来传递字符串params,它工作正常,但是如果我尝试使用json对象,在这里调用json对象时显示错误就是我的代码 private void makeJsonObjReq(){ showProgressDialog(); JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST, Const.URL\u登录,空, 新的Response.Listene
private void makeJsonObjReq(){
showProgressDialog();
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,
Const.URL\u登录,空,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
msgressponse.setText(response.toString());
hideProgressDialog();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
hideProgressDialog();
}
}) {
/**
*传递一些请求头
* */
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
返回标题;
}
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“un”xyz@gmail.com");
参数put(“p”,“somepasswordhere”);
返回参数;
}
};
//将请求添加到请求队列
AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);
//取消请求
//ApplicationController.getInstance().getRequestQueue().cancelAll(标记为json\u obj);
}
我的错误表单服务器是:[10031]基本网络。性能请求:意外响应代码401
如何解决这个问题。我想添加
application/json;标题中的charset=utf-8
请检查我的代码是否正确。请给我一个解决这个问题的建议jsonobject请求中的第三个参数是用于在jsonobject表单中传递post参数。对于标头,您需要发送两个单独的值,一个用于内容类型,一个用于字符集
RequestQueue queue = Volley.newRequestQueue(this);
private void makeJsonObjReq() {
showProgressDialog();
Map<String, String> postParam= new HashMap<String, String>();
postParam.put("un", "xyz@gmail.com");
postParam.put("p", "somepasswordhere");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.URL_LOGIN, new JSONObject(postParam),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
/**
* Passing some request headers
* */
@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;
}
};
jsonObjReq.setTag(TAG);
// Adding request to request queue
queue.add(jsonObjReq);
// Cancelling request
/* if (queue!= null) {
queue.cancelAll(TAG);
} */
}
RequestQueue queue=Volley.newRequestQueue(this);
私有void makeJsonObjReq(){
showProgressDialog();
Map postParam=新的HashMap();
后参数put(“un”xyz@gmail.com");
postParam.put(“p”,“somepasswordhere”);
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,
Const.URL\u登录,新JSONObject(postParam),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
msgressponse.setText(response.toString());
hideProgressDialog();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
hideProgressDialog();
}
}) {
/**
*传递一些请求头
* */
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
返回标题;
}
};
jsonObjReq.setTag(TAG);
//将请求添加到请求队列
add(jsonObjReq);
//取消请求
/*if(队列!=null){
queue.cancelAll(标记);
} */
}
创建新方法并在OnClick方法中调用它
public void PostOperation() {
requestQueue = Volley.newRequestQueue(this);
pdialog = new ProgressDialog(this);
pdialog.setMessage("Loading");
pdialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, "YOUR_URL",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pdialog.dismiss();
Log.e("login output", response);
//MODEL CLASS
LoginModel loginModel = new GsonBuilder().create().fromJson(response, LoginModel.class);
if (loginModel.getStatus().toString().equalsIgnoreCase("true")) {
Intent i = new Intent(context, DashboardActivity.class);
startActivity(i);
finish();
Toast.makeText(context, loginModel.getStatus() + "", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, loginModel.getMsg() + "", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pdialog.dismiss();
Toast.makeText(getApplicationContext(), "Invalid Credentials", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
HashMap<String, String> map = new HashMap<String, String>();
// pass your input text
map.put("email" editTextEmail.getText().toString());
map.put("password",editTextPassword.getText().toString() );
map.put("uid", "1");
map.put("type", "login");
Log.e("para", map + "");
return map;
}
};
requestQueue.add(stringRequest);
}
public void PostOperation(){
requestQueue=Volley.newRequestQueue(this);
pdialog=新建进度对话框(此对话框);
pdialog.setMessage(“加载”);
pdialog.show();
StringRequest StringRequest=新建StringRequest(Request.Method.POST,“您的URL”,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
pdialog.disclose();
Log.e(“登录输出”,响应);
//模范班
LoginModel LoginModel=new GsonBuilder().create().fromJson(响应,LoginModel.class);
if(loginModel.getStatus().toString().equalsIgnoreCase(“true”)){
意图i=新意图(上下文、仪表板活动.class);
星触觉(i);
完成();
Toast.makeText(上下文,loginModel.getStatus()+“”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(上下文,loginModel.getMsg()+“”,Toast.LENGTH_SHORT.show();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
pdialog.disclose();
Toast.makeText(getApplicationContext(),“无效凭据”,Toast.LENGTH\u SHORT.show();
}
}) {
@凌驾
受保护的映射getParams(){
HashMap=newHashMap();
//传递输入文本
map.put(“email”editTextEmail.getText().toString());
map.put(“password”,editTextPassword.getText().toString());
地图放置(“uid”、“1”);
map.put(“类型”、“登录”);
Log.e(“para”,map+”);
返回图;
}
};
添加(stringRequest);
}
JsonObjectRequest中的第三个参数
public void PostOperation() {
requestQueue = Volley.newRequestQueue(this);
pdialog = new ProgressDialog(this);
pdialog.setMessage("Loading");
pdialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, "YOUR_URL",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pdialog.dismiss();
Log.e("login output", response);
//MODEL CLASS
LoginModel loginModel = new GsonBuilder().create().fromJson(response, LoginModel.class);
if (loginModel.getStatus().toString().equalsIgnoreCase("true")) {
Intent i = new Intent(context, DashboardActivity.class);
startActivity(i);
finish();
Toast.makeText(context, loginModel.getStatus() + "", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, loginModel.getMsg() + "", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pdialog.dismiss();
Toast.makeText(getApplicationContext(), "Invalid Credentials", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
HashMap<String, String> map = new HashMap<String, String>();
// pass your input text
map.put("email" editTextEmail.getText().toString());
map.put("password",editTextPassword.getText().toString() );
map.put("uid", "1");
map.put("type", "login");
Log.e("para", map + "");
return map;
}
};
requestQueue.add(stringRequest);
}