2017-09-06 47 views
1

我试图发布请求,如下面的响应和获取错误{“消息”:“模型没有字段'对象''}我试图发布一个json字符串到服务器使用循环j并得到一个错误

{ 
    "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); 



     } 
    }); 

请花一分钟帮助,如果你知道这个错误... 感谢

+0

尝试使用jobj.put –

+0

jobj.put ?? .... –

+0

@Arslan阿里·请对象POJO类。 –

回答

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 
+0

让我检查这个 –

+0

发生同样的错误模型没有字段“对象” –

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字符串中删除所有"\"对邮差

0

检查JSON发布采购信息,如果你得到同样的错误,那么不工作请向您的api开发人员提供确切的Json Post格式,或告诉他们检查Post上后端的内容。因为这个错误信息是从服务器端显示的。

相关问题