[英]How to Post Application/Json Type using Retrofit and RxJava Android
I'm stuck with posting the just the title and location which is Array of lat and long, I need to check this. 我坚持只发布标题和位置,即lat和long数组,我需要检查一下。 I know this is not the enough context. 我知道这还不够。 I'll edit further please help me understanding this step by step. 我将进行进一步的编辑,请帮助我逐步了解这一点。
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("title","Dombivli");
JsonArray locationArray = new JsonArray();
locationArray.add(19.2093305);
locationArray.add(73.0645624);
jsonObject.add("location", locationArray);
subscription.add(createPointsOfViewModel.postAdd(jsonObject).subscribe(new Observer<PostPojo>() {
@Override
public void onCompleted() {
Toast.makeText(CreatePointsOfInterestActivity.this, "Complete", Toast.LENGTH_SHORT).show();
}
The Service method: 服务方法:
public interface CreatePointsOfInterestService {
@Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@POST("http://dev.citrans.net:8888/skymeet/swagger-ui.html#!/poi-controller/saveProductUsingPOST")
rx.Observable<PostPojo> postAdd(@Body JsonObject body);
}
The JSON I want to add in: 我要添加的JSON:
{
"createdAt": "2018-08-09T05:49:42.958Z",
"createdBy": "string",
"description": "string",
"id": "string",
"imageUrl": "string",
"location": [
0
],
"poiId": "string",
"status": "ACTIVE",
"title": "string",
"updatedAt": "2018-08-09T05:49:42.958Z",
"updatedBy": "string",
"version": 0
}
Your API URL is wrong. 您的API URL错误。 Seems you are passing the page URL instead of API URL. 似乎您正在传递页面URL而不是API URL。
Update your URL as follow: 如下更新您的URL:
@POST("http://dev.citrans.net:8888/skymeet/poi/add")
instead of 代替
@POST("http://dev.citrans.net:8888/skymeet/swagger-ui.html#!/poi-controller/saveProductUsingPOST")
Also, noted that in response it is giving 200 status code in Postman but there may be some problem in response from the server. 另外,请注意,作为响应,它在Postman中给出了200个状态代码,但是服务器的响应中可能存在一些问题。 So by replacing above API URL, your API will be called successfully but you have to check response with backend developer. 因此,通过替换上面的API URL,您的API将被成功调用,但是您必须与后端开发人员检查响应。
Send the full URL as @Url parameter in the interface, 在界面中将完整的URL作为@Url参数发送,
@POST
rx.Observable<PostPojo> postAdd(@Url String url, @Body JsonObject body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.