我试图使用循环j将json字符串发布到服务器并获取错误

时间:2017-09-06 06:40:04

标签: android json

我试图发布请求,如下面的响应并收到错误 {“message”:“模型没有字段'对象”'}

{
  "num_results": 20, 
  "objects": [
    {
      "agent": "LAPTOP_01", 
      "bluetooth_id": "33:22:55:44:FF:AA", 
      "created_at": "2017-09-05T18:02:00", 
      "device_name": "android_name", 
      "id": 1, 
      "location": "gps_location_info", 
      "name": "LAPTOP_01", 
      "raw": "['additional','info','goes','here']"
    }
  ], 
  "page": 1, 
  "total_pages": 2
}

这是我发布json字符串

的代码
AsyncHttpClient client = new AsyncHttpClient();
      String someData="{\"objects\": [{\"agent\": \"tsetLappy\", \"bluetooth_id\": \"33:25:55:44:FF:AA\", \"created_at\": \"2017-09-05:02:00\", \"device_name\": \"android_test\", \"location\": \"gps_info\", \"name\": \"testlappy\", \"raw\": \"['addl','inf','ges','hee']\"}]}";

    StringEntity entity = new StringEntity(someData);
    client.post(MainActivity.this, "http://impero-dannyfast.rhcloud.com/api/agents", entity, "application/json", new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);



         status.setText("Status: Posted Successfully!");


        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);

           status.setText("Status: Error in Sending Data!"+errorResponse);



        }
    });

如果你知道这个错误,请花点时间帮忙... 感谢

3 个答案:

答案 0 :(得分:0)

    JSONObject obj = new JSONObject();
                               try
                               {
                                obj .put("num_results", your_value);
                                obj .put("page", your_value);
                                obj .put("pages", your_value);
    JSONArray jarray = new JSONArray();
    JSONObject objects_obj = new JSONObject();

                               objects_obj .put("agent", your_value);
    ....
    .
    .
    .
    jarray.put(objects_obj);
    obj.put("objects",jarray);
                               } catch (JSONException e) {
                                e.printStackTrace();
                            }
//now pass this obj in request

答案 1 :(得分:0)

试试这个:

   ByteArrayEntity entity = null;

            try {

             String someData="{\"objects\": [{\"agent\": \"tsetLappy\", \"bluetooth_id\": \"33:25:55:44:FF:AA\", \"created_at\": \"2017-09-05:02:00\", \"device_name\": \"android_test\", \"location\": \"gps_info\", \"name\": \"testlappy\", \"raw\": \"['addl','inf','ges','hee']\"}]}";


                entity = new ByteArrayEntity(someData.getBytes("UTF-8"));

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


            client.post(context, requestUrl, entity, "application/json", new JsonHttpResponseHandler() {
          }

如果上述方法无效,请尝试从json字符串中删除所有"\"

答案 2 :(得分:0)

检查邮递员的json Post请求,如果您收到同样的错误,请向您的api开发人员询问确切的Json Post格式,或者告诉他们检查他从Post发送到后端的内容。因为从服务器端显示此错误消息。