且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Json作为字符串发送到服务器(nginx)-改造2

更新时间:2023-01-06 07:39:23

我没有使用 Retrofit 2.+ 的经验,但是在 Retrofit 1.9 中发送jsonPOST请求中的String的身份发送到服务器,我***将其封装在TypedJsonString

I have no experience with Retrofit 2.+, but in Retrofit 1.9 to send json to server as a String in a POST request I have been forced to envelop it in TypedJsonString

import retrofit.mime.TypedString;

public class TypedJsonString extends TypedString {
    public TypedJsonString(String body) {
        super(body);
    }

    @Override public String mimeType() {
        return "application/json";
    }
}

并使用此接口定义:

@POST(BASE_METHOD)
Response postProductRequest(@Body TypedJsonString productRequest); 

从后台线程发出请求:

String requestBody = new Gson().toJson(new ProductRequest());
retrofit.client.Response r = mApiService.postProductRequest(new TypedJsonString(requestBody));