我想發佈這樣的郵政原始JSON
{ 「jsonrpc」 JSON請求: 「2.0」, 「方法」: 「testApi」, 「PARAMS」:{ 「消息」: 「ABC」 } , 「ID」:1}
我看了帖子:
How to POST raw whole JSON in the body of a Retrofit request?
但我can'not發現了類TypedInput,TypedByteArray,TypedString我retrofit2包。哪裏?
我想發佈這樣的郵政原始JSON
{ 「jsonrpc」 JSON請求: 「2.0」, 「方法」: 「testApi」, 「PARAMS」:{ 「消息」: 「ABC」 } , 「ID」:1}
我看了帖子:
How to POST raw whole JSON in the body of a Retrofit request?
但我can'not發現了類TypedInput,TypedByteArray,TypedString我retrofit2包。哪裏?
張貼身體Retrofit
,您創建一個對象,代表本機構,包括String jsonrpc
類,String method
等。然後,這個對象傳遞給您在服務接口定義,並與設置了一個param方法@Body
註釋。
這裏是POST主體對象的例子:
public class PostBody{
String jsonprc;
String method;
Param param;
public PostBody(...){
//IMPLEMENT THIS
}
...
class Param{
//IMPLEMENT THIS
}
}
POST數據到服務器需要backhend程序這是發表您的數據在服務器上的數據庫 .....
改造後需要RESTAPI和POJO類....
API接口
public interface Api {
@POST("/upload/{new}.json")
Call<User> setData(@Path("new") String s1, @Body User user);
}
改造對象
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("here-your-url")
.addConverterFactory(GsonConverterFactory.create())
.build();
API對象
Api api = retrofit.create(Api.class);
改造呼叫
Call<User> call = api.setData("mahesh", new User("mahesh", "delhi"));
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
t1.setText("Success");
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Log.d("sam", "fail");
t1.setText("fail");
}
});
POJO類//這個類,你創建只是把你的JSON數據到這個POJOConvertion
public class User {
String name;
String address;
public User(String name, String address) {
this.address = address;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
享受編碼。
如果要用於改造任何做法比請使用此Retrofit + Firebase
感謝兄弟。幫助過我。 –
我怎麼能代表PARAMS JSON數組(從我的例子)類領域? –
如果該對象除了該主體以外的任何地方不需要,則爲其創建靜態內部類。 – Embydextrous
查看我的增補 –