android - 通过 Volley 无法使用 POST 将数据发送到服务器

标签 android json android-volley

刚刚在Volley中遇到了一个奇怪的问题。
首先,我使用 Apache Http 库测试了以下代码,并取得了成功,尝试使用 Postman Client 并取得了成功,但每次在 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;
    }

//工作得很好 这是来自 POSTMAN CLIENT:

enter image description here

//成功

这是 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) {

            }
        })

如有任何帮助,我们将不胜感激。

最佳答案

您使用了错误的参数。如果您阅读文档,您会发现您不需要 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>()

关于android - 通过 Volley 无法使用 POST 将数据发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38806941/

相关文章:

即使进行了所有优化,Android Emulator 也会延迟

javascript - 格式化 JSON 以进行 Angular 数据绑定(bind)

c# - Google 财经,如何获取流式传输的 JSON 数据?

c# - ASP.NET Core 3.0 [FromBody] 字符串内容返回 "The JSON value could not be converted to System.String."

android - Android开发使用Volley时setRetryPolicy()方法调用应该写在哪里

android - 无法创建ViewModel类的实例

android - 在 Android 中单击 FCM 通知后打开 Activity

android - 如何添加两个android :name attributes to Application tag in Manifest file?

Android Volley 给我 400 错误

android - Ionic 应用程序 - 无法访问互联网