android - Retrofit Post json 遇到问题
伊谢尔伦
伊谢尔伦 2017-04-17 17:54:03
0
1
429

比如这样的接口
http://www.xxx.com/xxx/xxx

post方式传

 token="xxx"
 param="{user:{id:xxx,name:xxx},order:{id:xxx}}"

用retrofit 该怎么传?

我现在的方法
表单形式上传

@Field("token") string token
@Field("param") string param
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
迷茫

Define the data to be requested as an entity class

public class CustomBody {

    public String token;
    public User user;
    public Order order;

    public CustomBody(String token, User user, Order order) {
        this.token = token;
        this.user = user;
        this.order = order;
    }

    public static class Order {
        public String id;

        public Order(String id) {
            this.id = id;
        }
    }

    public static class User {
        public String id;
        public String name;

        public User(String id, String name) {
            this.id = id;
            this.name = name;
        }
    }

}

Retrofit’s Service can be written like this, using @Body annotations

public interface MyService {

    @POST("/")
    Observable<T> access(@Body CustomBody customBody);
}

Use HttpLoggingInterceptor to view the details of HTTP requests in the log.

OkHttpClient.interceptors().add(new HttpLoggingInterceptor());
11-19 16:58:46.495 4591-4612/? D/OkHttp: --> POST / HTTP/1.1
11-19 16:58:46.495 4591-4612/? D/OkHttp: {"order":{"id":"1"},"token":"123","user":{"id":"1","name":"name"}}
11-19 16:58:46.495 4591-4612/? D/OkHttp: --> END POST (66-byte body)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template