我已经创建了我的模型,如
package com.bertho.tdashboard.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Visitor {
@SerializedName("nb_uniq_visitors")
@Expose
private Integer nbUniqVisitors;
@SerializedName("nb_users")
@Expose
private Integer nbUsers;
@SerializedName("nb_visits")
@Expose
private Integer nbVisits;
@SerializedName("nb_actions")
@Expose
private Integer nbActions;
@SerializedName("nb_visits_converted")
@Expose
private Integer nbVisitsConverted;
@SerializedName("bounce_count")
@Expose
private Integer bounceCount;
@SerializedName("sum_visit_length")
@Expose
private Integer sumVisitLength;
@SerializedName("max_actions")
@Expose
private Integer maxActions;
@SerializedName("bounce_rate")
@Expose
private String bounceRate;
@SerializedName("nb_actions_per_visit")
@Expose
private Double nbActionsPerVisit;
@SerializedName("avg_time_on_site")
@Expose
private Integer avgTimeOnSite;
/**
* @return The nbUniqVisitors
*/
public Integer getNbUniqVisitors() {
return nbUniqVisitors;
}
/**
* @param nbUniqVisitors The nb_uniq_visitors
*/
public void setNbUniqVisitors(Integer nbUniqVisitors) {
this.nbUniqVisitors = nbUniqVisitors;
}
/**
* @return The nbUsers
*/
public Integer getNbUsers() {
return nbUsers;
}
/**
* @param nbUsers The nb_users
*/
public void setNbUsers(Integer nbUsers) {
this.nbUsers = nbUsers;
}
/**
* @return The nbVisits
*/
public Integer getNbVisits() {
return nbVisits;
}
/**
* @param nbVisits The nb_visits
*/
public void setNbVisits(Integer nbVisits) {
this.nbVisits = nbVisits;
}
/**
* @return The nbActions
*/
public Integer getNbActions() {
return nbActions;
}
/**
* @param nbActions The nb_actions
*/
public void setNbActions(Integer nbActions) {
this.nbActions = nbActions;
}
/**
* @return The nbVisitsConverted
*/
public Integer getNbVisitsConverted() {
return nbVisitsConverted;
}
/**
* @param nbVisitsConverted The nb_visits_converted
*/
public void setNbVisitsConverted(Integer nbVisitsConverted) {
this.nbVisitsConverted = nbVisitsConverted;
}
/**
* @return The bounceCount
*/
public Integer getBounceCount() {
return bounceCount;
}
/**
* @param bounceCount The bounce_count
*/
public void setBounceCount(Integer bounceCount) {
this.bounceCount = bounceCount;
}
/**
* @return The sumVisitLength
*/
public Integer getSumVisitLength() {
return sumVisitLength;
}
/**
* @param sumVisitLength The sum_visit_length
*/
public void setSumVisitLength(Integer sumVisitLength) {
this.sumVisitLength = sumVisitLength;
}
/**
* @return The maxActions
*/
public Integer getMaxActions() {
return maxActions;
}
/**
* @param maxActions The max_actions
*/
public void setMaxActions(Integer maxActions) {
this.maxActions = maxActions;
}
/**
* @return The bounceRate
*/
public String getBounceRate() {
return bounceRate;
}
/**
* @param bounceRate The bounce_rate
*/
public void setBounceRate(String bounceRate) {
this.bounceRate = bounceRate;
}
/**
* @return The nbActionsPerVisit
*/
public Double getNbActionsPerVisit() {
return nbActionsPerVisit;
}
/**
* @param nbActionsPerVisit The nb_actions_per_visit
*/
public void setNbActionsPerVisit(Double nbActionsPerVisit) {
this.nbActionsPerVisit = nbActionsPerVisit;
}
/**
* @return The avgTimeOnSite
*/
public Integer getAvgTimeOnSite() {
return avgTimeOnSite;
}
/**
* @param avgTimeOnSite The avg_time_on_site
*/
public void setAvgTimeOnSite(Integer avgTimeOnSite) {
this.avgTimeOnSite = avgTimeOnSite;
}
}
和我的回应
package com.bertho.tdashboard.model;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class VisitorResponse {
private List<Visitor> visitors;
public List<Visitor> getVisitors() {
return visitors;
}
public void setItems(List<Visitor> visitors) {
this.visitors = visitors;
}
}
但每次我尝试调用API时,我得到的结果始终为null
。模型和我的反应是否有问题?
如何阅读日期数据以及要检索并显示在屏幕上的详细信息。
上面的API格式是否可以成为模型,或者是否应该更改此API格式?
请帮忙
答案 0 :(得分:0)
1. Activity Code :
public class MainActivity extends AppCompatActivity implements ServiceCallBack {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BaseRequest baseRequest = new BaseRequest(MainActivity.this);
baseRequest.setServiceCallBack(MainActivity.this);
baseRequest.setRequestTag(RestClient.LOGIN_SERVICE_TAG);
Api api = (Api) baseRequest.execute(Api.class);
if (api != null) {
api.getData(baseRequest.requestCallback());
}
}
@Override
public void onSuccess(int tag, String baseResponse) {
Log.e("onSuccess", "onSuccess" + baseResponse.toString());
}
@Override
public void onFail(RetrofitError error) {
Log.e("onFail", "onFail" + error.toString());
}
}
Api界面:
public interface Api {
@GET( “/测试”) public void getData(Callback callback);
}
3. Base Request :
public class BaseRequest {
private int requestTag;
private boolean progressShow = true;
private ServiceCallBack serviceCallBack;
private RestClient restClient;
private Context context;
private Callback<Object> callback;
public BaseRequest(Context context){
this.context = context;
setProgressShow(true);
}
public Object execute(Class classes){
restClient= new RestClient(context,this);
return restClient.execute(classes);
}
public Callback<String> requestCallback(){
return restClient.callback;
}
public Callback<Object> getCallback() {
return callback;
}
public void setCallback(Callback<Object> callback) {
this.callback = callback;
}
public RestClient getRestClient() {
return restClient;
}
public void setRestClient(RestClient restClient) {
this.restClient = restClient;
}
public ServiceCallBack getServiceCallBack() {
return serviceCallBack;
}
public void setServiceCallBack(ServiceCallBack serviceCallBack) {
this.serviceCallBack = serviceCallBack;
}
public int getRequestTag() {
return requestTag;
}
public void setRequestTag(int requestType) {
this.requestTag = requestType;
}
public boolean isProgressShow() {
return progressShow;
}
public void setProgressShow(boolean progressShow) {
this.progressShow = progressShow;
}
}
4. Service CallBack Interface :
public interface ServiceCallBack{
public void onSuccess(int tag, String baseResponse);
public void onFail(RetrofitError error);
}
5. RestClient :
public class RestClient {
public static String BASE_URL = "http://www.berthojoris.com/";
public static int LOGIN_SERVICE_TAG = 1001;
private BaseRequest baseRequest;
private ProgressDialog pDialog = null;
private Context context;
public RestClient(Context context, BaseRequest _baseRequest) {
this.baseRequest = _baseRequest;
this.context = context;
}
public Object execute(Class classes) {
if (baseRequest.isProgressShow())
showProgressDialog(context);
return setUpRestClient(classes);
}
private Object setUpRestClient(Class apiClasses) {
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(120, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(120, TimeUnit.SECONDS);
RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.setClient(new OkClient(okHttpClient));
builder.setConverter(new StringConverter());
RestAdapter restAdapter = builder.build();
return restAdapter.create(apiClasses);
}
public Callback<String> callback = new Callback<String>() {
@Override
public void success(String o, Response response) {
if (o != null && !o.isEmpty())
baseRequest.getServiceCallBack().onSuccess(baseRequest.getRequestTag(), o);
dismissProgressDialog();
}
@Override
public void failure(RetrofitError error) {
baseRequest.getServiceCallBack().onFail(error);
dismissProgressDialog();
}
};
public void showProgressDialog(Context context) {
try {
if (((Activity) context).isFinishing()) {
return;
}
pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void dismissProgressDialog() {
try {
if (pDialog != null)
pDialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
class StringConverter implements Converter {
@Override
public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
String text = null;
try {
text = fromStream(typedInput.in());
} catch (IOException ignored) {
ignored.printStackTrace();
}
return text;
}
@Override
public TypedOutput toBody(Object o) {
return null;
}
private String fromStream(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String newLine = System.getProperty("line.separator");
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append(newLine);
}
return out.toString();
}
}
}
答案 1 :(得分:-1)
如果您在字段上使用@Expose,则告诉Gson将该属性包含在您的JSON字符串中。所有未使用@Expose注释的字段都将被排除
仅使用@SerializedName()并检查以下链接: