http传输,post请求使用form表单的形式的话是在api请求中用@fIeld 字段
如果使用body传输,则如下:
1.post请求
@POST("/api/v1/user")fun getUser(@Body body: RequestBody?): Observable<BaseBean<String>>
2.model中:
fun attentionMachine(params: HashMap<String, Any>): Observable<BaseBean<String>> {return RetrofitManager.service.attentionMachine(ParamsUtils.getRequestBody(params )).compose(SchedulerUtils.ioToMain())}
3,ParamsUtils
import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.RequestBodyobject ParamsUtils {fun getRequestBody(params: HashMap<String, Any>?): RequestBody {//post请求或者get请求将请求参数放进这里val bodyMap = HashMap<String, Any>()bodyMap["Device"] = "android"bodyMap["App"] = "appName"//其他参数params?.let {bodyMap.putAll(it)}return RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Gson().toJson(bodyMap))}}