2
我想知道如何正確使用GSon和翻新? 我從服務器接收到的JSON:如何正確使用GSon和翻新
{"tag":"getall","success":1,"error":0,"messages":[
{"message":"aaa","user_id":"111e9f66624637.88881557","time":"2014-10-17 21:27:29","usermail":"[email protected]"},{"message":"hdhxnd","user_id":"111e9f66624637.88881557","time":"2014-10-17 21:14:36","usermail":"[email protected]"},{"message":"zz","user_id":"111e9f66624637.88881557","time":"2014-10-17 21:11:15","usermail":"[email protected]"},{"message":"d","user_id":"111e9f66624637.88881557","time":"2014-10-17 18:16:35","usermail":"[email protected]"},{"message":"c","user_id":"111e9f66624637.88881557","time":"2014-10-17 18:15:16","usermail":"[email protected]"}]}
這是我的改裝方法:
public interface ConnectInterface {
@FormUrlEncoded
@POST("/")
void getUserMessages(@Field("tag") String first, @Field("device_id") String last, Callback<MyResponse> cb);
}
MyResponse類:
public class MyResponse {
public List<Messages> messages;
public String success;
public String error;
public String tag;
public class Messages {
public String message;
public String user_id;
public String time;
public String usermail;
}
}
和代碼的一部分,其使用它:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(URL)
.setErrorHandler(new MyErrorHandler())
.build();
ConnectInterface service = restAdapter.create(ConnectInterface.class);
Callback<MyResponse> stringCallback = new Callback<MyResponse>() {
@Override
public void success(MyResponse s, Response response) {
System.out.println(s.tag);
for(MyResponse.Messages messages: s.messages){
System.out.println(messages.message);
}
}
@Override
public void failure(RetrofitError error) {
System.out.println(error.toString());
}
};
service.getUserMessages("getall", "4", stringCallback);
我是否以一種好的方式去做?什麼是
Gson gson = new GsonBuilder()
setConverter(new GsonConverter(gson))
這些方法?
你解決問題了嗎? – pengwang 2014-12-01 08:21:42