【问题标题】:Sending data to server with POST through Volley not working通过 Volley 使用 POST 将数据发送到服务器不起作用
【发布时间】:2016-08-07 01:52:41
【问题描述】:

刚刚在 Volley 中遇到了一个奇怪的问题。
首先,我使用 Apache Http 库测试了下面的代码,并获得了成功,尝试使用 Postman 客户端并获得了成功,但每次在 Volley 中,我都会收到关于 JsonString can't be converted to Json Object 的解析错误。

这是我的工作代码:
使用旧的 Apache HTTP LIB:

httpClient=new DefaultHttpClient();
                StringBuilder stringBuilder=new StringBuilder(confirm_url);
                httpPost=new HttpPost(stringBuilder.toString());
                new webLogin().execute();
            try {
            jsonObject=new JSONObject();

            try {

                jsonObject.put("customerID",long_customerID);//long
                jsonObject.put("restaurantId",rcv_details_rcv_restaurantId);//long
                jsonObject.put("subscriptionPlan",rcv_details_subscriptionPlan);
                jsonObject.put("subscriptionDays",rcv_details_rcv_subscriptionDays);//int
                jsonObject.put("subscriptionAmount",rcv_details_subscriptionAmount);//int

                jsonObject.put("kidName",kid_name);
                jsonObject.put("clas23",rcv_class);
                jsonObject.put("section",rcv_section);
                jsonObject.put("gender",rcv_gender);
                DateTime obj=new DateTime();
                DateTime dateTime=new DateTime();
                jsonObject.put("startDate",dateTime);
                jsonObject.put("schoolName",rcv_school);
                jsonObject.put("address",rcv_delivery);

                jsonObject.put("paymentType",paymentType);
                jsonObject.put("restaurantSubscriptionId",rcv_details_rcv_restaurantSubscriptionId);//long
                jsonObject.put("subscriptionId",0);//long


            } catch (JSONException e) {
                e.printStackTrace();
            }
            stringEntity=new StringEntity(jsonObject.toString());
            httpPost.setEntity(stringEntity);
            httpPost.setHeader("Content-type", "application/json");

            httpResponse=httpClient.execute(httpPost);
            int statusCode=httpResponse.getStatusLine().getStatusCode();


            if(statusCode==200){

                entity = httpResponse.getEntity();
                System.out.println("Entity post is: "
                        + EntityUtils.toString(entity));
                mre = "200";
                Log.d("SUCCESS","YES FINALLY");
                Log.d("Ok",entity.toString());

            }else if (statusCode==412){
                Log.d("412", "412 WE MEET AGAIN)");
             mre = "412";

            }else
                Log.i("Unknown","Unknown Server Error");
            mre="unknown";

        } catch (IOException e) {
            e.printStackTrace();
        }

        return mre;
    }

//工作正常 这是来自邮递员客户:

//成功

这是 Volley(错误:字符串无法转换为 JSON 对象)

jsonObject=new JSONObject();
    try {

        //I put it manually not through SHARED PREF

        jsonObject.put("customerID",long_customerID);//long
        jsonObject.put("restaurantId",rcv_details_rcv_restaurantId);//long
        jsonObject.put("subscriptionPlan",rcv_details_subscriptionPlan);
        jsonObject.put("subscriptionDays",rcv_details_rcv_subscriptionDays);//int
        jsonObject.put("subscriptionAmount",rcv_details_subscriptionAmount);//int


        jsonObject.put("kidName",kid_name);
        jsonObject.put("clas23",rcv_class);
        jsonObject.put("section",rcv_section);
        jsonObject.put("gender",rcv_gender);
        *//*jsonObject.put("startDate",String.valueOf(rcv_date));*//*
      *//*  DateTime obj=new DateTime();*//*
        DateTime dateTime=new DateTime();
        Log.i("ffds",dateTime.toString());

        jsonObject.put("startDate",dateTime.toString());
        jsonObject.put("schoolName",rcv_school);
        jsonObject.put("address",rcv_delivery);

        jsonObject.put("paymentType",paymentType);
        jsonObject.put("restaurantSubscriptionId",rcv_details_rcv_restaurantSubscriptionId);//long
        jsonObject.put("subscriptionId",subscriptionId);//long



        requestBody=jsonObject.toString();
        Log.i("Daa",jsonObject.toString());

        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, confirm_url, requestBody, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response_jsonObject) {
                Log.i("Login Response",response_jsonObject.toString());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        })

任何帮助将不胜感激。

【问题讨论】:

    标签: android json android-volley


    【解决方案1】:

    您使用了错误的参数。如果您阅读文档,您会发现您不需要 toString JsonObject。

    您甚至不需要 Method 参数。

    试试这个 POST

    new JsonObjectRequest(confirm_url, jsonObject, new Response.Listener<JSONObject>()
    

    这对于 GET(JsonObject 为空,因为 GET 请求中没有正文)

    new JsonObjectRequest(confirm_url, null, new Response.Listener<JSONObject>()
    

    【讨论】:

    • 您是否建议不要使用请求正文先生?
    • JsonObject 是请求体。它不需要是字符串
    • new JsonObjectRequest(Request.Method.POST,confirm_url, jsonObject, new Response.Listener() 这是正确的!
    • 尝试找出答案。我已经在我的答案中给出了正确的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多