Skip to content

Json请求

liujingxing edited this page Feb 25, 2023 · 2 revisions

post Json对象请求

//发送以下Json对象                                                                   
/*                                                                             
   {                                                                           
       "name": "张三",                                                           
       "sex": 1,                                                               
       "height": 180,                                                          
       "weight": 70,                                                           
       "interest": [                                                           
           "羽毛球",                                                              
           "游泳"                                                                
       ],                                                                      
       "location": {                                                           
           "latitude": 30.7866,                                                
           "longitude": 120.6788                                               
       },                                                                      
       "address": {                                                            
           "street": "科技园路.",                                                  
           "city": "江苏苏州",                                                     
           "country": "中国"                                                     
       }                                                                       
   }                                                                           
 */                                                                            
List<String> interestList = new ArrayList<>();//爱好                             
interestList.add("羽毛球");                                                       
interestList.add("游泳");                                                        
String json = "{\"height\":180,\"weight\":70}";
String address = "{\"street\":\"科技园路.\",\"city\":\"江苏苏州\",\"country\":\"中国\"}";

                                                                               
RxHttp.postJson("/article/list/0/json")                                        
    .add("name", "张三")                                                         
    .add("sex", 1)                                                             
    .addAll(json) //通过addAll系列方法添加多个参数             
    .add("interest", interestList) //添加数组对象                                    
    .add("location", new Location(120.6788, 30.7866))  //添加位置对象                
    .addJsonElement("address", address) //通过字符串添加一个对象                          
    .toObservableString()                                                                
    .subscribe(s -> {
        //成功回调
    }, throwable -> {
        //失败回调
    });                                                                     

post Json数组请求

//发送以下Json数组                                
/*                                          
   [                                        
       {                                    
           "name": "张三"                     
       },                                   
       {                                    
           "name": "李四"                     
       },                                   
       {                                    
           "name": "王五"                     
       },                                   
       {                                    
           "name": "赵六"                     
       },                                   
       {                                    
           "name": "杨七"                     
       }                                    
   ]                                        
 */                                         
List<Name> names = new ArrayList<>();       
names.add(new Name("赵六"));                  
names.add(new Name("杨七"));   

String json = "{\"name\":\"王五\"}";

RxHttp.postJsonArray("/article/list/0/json")
    .add("name", "张三")                       
    .add(new Name("李四"))                     
    .addJsonElement(json)     
    .addAll(names)                           
    .toObservableString()                              
    .subscribe(s -> {
        //成功回调
    }, throwable -> {
        //失败回调
    });          
    
public class Name {

    String name;

    public Name(String name) {
        this.name = name;
    }
}