如何使用Volley Android Java将json数据插入服务器?

如何使用Volley Android Java将json数据插入服务器?,android,json,android-volley,Android,Json,Android Volley,使用授权令牌,这是JSON数据 { “费用”:77, “一揽子计划”:{ “类型”:“小”, “重量”:3, “长度”:18, “宽度”:12, “高度”:5 }, “项目”:[ {“姓名”:“全部”,“描述”:“全部”,“价值”:“1000”,“付款”:假} ], “收货信息”:{ “姓名”:“约翰·威克”, “联系人”:“09123123”, “地址”:“完整,地址”, “特定地址”:“桥接” }, “交货信息”:{ “姓名”:“约翰·威克2号”, “联系人”:“09321”, “地址”:“

使用授权令牌,这是JSON数据

{ “费用”:77, “一揽子计划”:{ “类型”:“小”, “重量”:3, “长度”:18, “宽度”:12, “高度”:5 }, “项目”:[ {“姓名”:“全部”,“描述”:“全部”,“价值”:“1000”,“付款”:假} ], “收货信息”:{ “姓名”:“约翰·威克”, “联系人”:“09123123”, “地址”:“完整,地址”, “特定地址”:“桥接” }, “交货信息”:{ “姓名”:“约翰·威克2号”, “联系人”:“09321”, “地址”:“完整,地址2”, “地址特定”:“Purok橙色” } }

RequestQueue RequestQueue=Volley.newRequestQueue(this)

JSONObject object=new JSONObject();
JSONObject包=新的JSONObject();
试一试{
反对。付诸表决(“费用”,77);
}捕获(JSONException e){
e、 printStackTrace();
}
试一试{
包装。放置(“类型”、“小”);
包装。放置(“重量”、“3”);
包装。放置(“长度”、“18”);
包装。放置(“宽度”、“12”);
包装。放置(“高度”、“5”);
对象。放置(“包”,包);
}捕获(JSONException e){
e、 printStackTrace();
}
//项目
JSONObject items=新的JSONObject();
试一试{
项目。填写(“名称”、“全部”);
项目。投入(“说明”、“全部”);
项目。投入(“价值”,“1000”);
项目。投入(“付款”、“虚假”);
JSONArray JSONArray=新的JSONArray();
jsonArray.put(项目);
object.put(“items”,jsonArray);
}捕获(JSONException e){
e、 printStackTrace();
}
//皮卡
JSONObject拾取_info=新建JSONObject();
试一试{
收货信息栏(“名称”、“样品”);
皮卡信息输入(“联系人”,“0912121212”);
收货信息输入(“地址”,“完成,宿务”);
收件信息输入(“特定地址”、“桥接”);
放置(“拾取信息”,拾取信息);
}捕获(JSONException e){
e、 printStackTrace();
}
//交付
JSONObject delivery_info=新JSONObject();
试一试{
交付信息栏(“名称”、“样品2”);
交付信息放置(“联系人”,“012121212”);
交付信息输入(“地址”,“完成,宿务2”);
交付信息投入(“特定地址”、“桥2”);
object.put(“交货信息”,交货信息);
}捕获(JSONException e){
e、 printStackTrace();
}
字符串URL=”https://www.jogx.ph/api/v1/transaction/createOrder";
StringRequest=newStringRequest(request.Method.POST,URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG.show();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“截击”,error.toString());
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
返回参数;
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
String您的_String_json=object.toString();//放入您的json
返回您的_字符串_json.getBytes();
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map headers=newhashmap();
headers.put(“内容类型”、“应用程序/x-www-form-urlencoded”);
headers.put(“authToken”、“token”);
返回标题;
}
};
添加(请求);

尝试使用JsonObjectRequest而不是StringRequest

您应该更改代码

String URL = "https://www.jogx.ph/api/v1/transaction/createOrder";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,URL,object,new Response.Listener<JSONObject>(){
   @Override
   public void onResponse(JSONObject response) {
                Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
   }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
              Log.e("VOLLEY",error.toString());
   }
}){
    @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("content-type", "application/x-www-form-urlencoded");
            headers.put("authToken","yourToken");
            return headers;
        }
};
requestQueue.add(request);

stringurl=”https://www.jogx.ph/api/v1/transaction/createOrder";
JsonObjectRequest=newJSONObjectRequest(request.Method.POST、URL、object、new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG.show();
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“截击”,error.toString());
}
}){
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map headers=newhashmap();
headers.put(“内容类型”、“应用程序/x-www-form-urlencoded”);
headers.put(“authToken”、“yourToken”);
返回标题;
}
};
添加(请求);

另外,您不应该在问题中共享身份验证令牌。

您到目前为止尝试了什么?您应该添加到这里,先生,我编辑我的帖子,我正在使用volleyHi先生,它会给我一条错误消息E/VOLLEY:com.android.VOLLEY.ParseError:org.json.JSONException:Value,异常发生在哪里?您可以共享堆栈跟踪吗?
String URL = "https://www.jogx.ph/api/v1/transaction/createOrder";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,URL,object,new Response.Listener<JSONObject>(){
   @Override
   public void onResponse(JSONObject response) {
                Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
   }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
              Log.e("VOLLEY",error.toString());
   }
}){
    @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("content-type", "application/x-www-form-urlencoded");
            headers.put("authToken","yourToken");
            return headers;
        }
};
requestQueue.add(request);