gpt4 book ai didi

android - 如何在android中使用volley将json对象发送到服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:54:54 24 4
gpt4 key购买 nike

I want to send the JSONObject to server using POST method . I have used volley library to pass the string params its working fine, but if i try to use json object its showing an error for calling the json object here is my code

    private void makeJsonObjReq() {
showProgressDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.URL_LOGIN, null,
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;
}

@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("un", "xyz@gmail.com");
params.put("p", "somepasswordhere");
return params;
}

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);

// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
}

And my Error form server is : [10031] BasicNetwork.performRequest: Unexpected response code 401

如何解决这个问题。我想在 header 中添加 application/json;charset=utf-8 请检查我的代码是否正确。请给我一个解决这个问题的建议

最佳答案

JsonObjectRequest 中的第三个参数用于传递 jsonobject 形式的 post 参数。对于 header ,您需要发送两个单独的值,一个用于内容类型,一个用于字符集。

  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);
} */

}

关于android - 如何在android中使用volley将json对象发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28344448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com