2016-08-05 68 views

回答

2

張貼身體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 
    } 

} 
+0

我怎麼能代表PARAMS JSON數組(從我的例子)類領域? –

+0

如果該對象除了該主體以外的任何地方不需要,則爲其創建靜態內部類。 – Embydextrous

+0

查看我的增補 –

3

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

+0

感謝兄弟。幫助過我。 –