下午,
我有这段代码:
{
"criteriaelement":
[
{
"Value1" : "03",
"FieldName" : "InventLocationId",
"Operator" : 0,
"DataSourceName" : "SalesTable"
}
]
}
public JSONObject crear_json_object(){
JSONObject param= new JSONObject();
JSONArray criteria_e= new JSONArray();
JSONObject param_obj= new JSONObject();
//agregamos los parametros al JSONOBJECT
try{
param_obj.put("Value1","03");
param_obj.put("Operator",0);
param_obj.put("DataSourceName","SalesTable");
param_obj.put("FieldName","InventLocationId");
criteria_e.put(param_obj);
param.put("criteriaelement", criteria_e);
msgResponse.setText(param.toString());
}catch(Exception e){}
return param;
}
private void get_sales_orders() {
//para enviar objetos json en lugar de null se deben enviar...
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.ws_Salesorders, crear_json_object(),
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();
}
}) {
//@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer "+access_token;
return headers;
}
};
// Adding request to request queue
myapplication.getInstance().addToRequestQueue(jsonObjReq,
"sales");
}
我的jsonobject形成良好(我在google chrome的POST扩展中测试了这个),当我在这个扩展中使用这个Web服务时,只是工作...
但是,将它传递给Volley,我收到一个httpstatus代码400,意味着这是一个糟糕的请求...... 有人可以帮忙吗??我不知道为什么它不起作用,我错过了什么?
答案 0 :(得分:4)
这就是我所需要的:
JSONObject parameters = new JSONObject();
try {
parameters.put("key", "value");
} catch (Exception e) {
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, yourUrl, parameters,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("onResponse", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("onErrorResponse", error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
// Basic Authentication
//String auth = "Basic " + Base64.encodeToString(CONSUMER_KEY_AND_SECRET.getBytes(), Base64.NO_WRAP);
headers.put("Authorization", "Bearer " + accesstoken);
return headers;
}
};
queue.add(request);