我正在进行四天的改造工作。我学会了如何从Web服务获取数据,但现在我坚持这个响应。请任何人告诉我如何从Web服务获取数据。如何获取运算符id,运算符,RechareTypes字符串数组。请任何人帮助我。提前致谢。
我的代码: 操作者:
public class Operator {
@Expose
private String OperatorID="";
@Expose
private String ServiceTypeID="";
public String getServiceTypeID() {
return ServiceTypeID;
}
public void setServiceTypeID(String serviceTypeID) {
ServiceTypeID = serviceTypeID;
}
public String getOperatorID() {
return OperatorID;
}
public void setOperatorID(String operatorID) {
OperatorID = operatorID;
}
public String getOperatorname() {
return operatorname;
}
public List<Operators> getOrderItems() {
return OrderItems;
}
public void setOrderItems(List<Operators> orderItems) {
OrderItems = orderItems;
}
public void setOperatorname(String operatorname) {
this.operatorname = operatorname;
}
@Expose
private String operatorname="";
@Expose
private List<Operators> OrderItems = new ArrayList<Operators>();
public Operator(){
}
}
运算符:
public class Operators {
private String RechargeTypeID="";
public String getRechargeType() {
return RechargeType;
}
public void setRechargeType(String rechargeType) {
RechargeType = rechargeType;
}
public String getSMSCode() {
return SMSCode;
}
public void setSMSCode(String SMSCode) {
this.SMSCode = SMSCode;
}
public String getRechargeTypeID() {
return RechargeTypeID;
}
public void setRechargeTypeID(String rechargeTypeID) {
RechargeTypeID = rechargeTypeID;
}
private String RechargeType="";
private String SMSCode="";
}
活性:
ArrayList<Operator> operator;
void executeCircleAndOperatorsList() {
db.ClearTables();
ServiceClient serviceClient = ServiceUtil.getServiceClient();
JsonParser jsonParser = new JsonParser();
serviceClient.getOperators("getRecharge_Operator_v2", operatorCallback);
}
Callback<JsonObject> operatorCallback = new Callback<JsonObject>() {
@Override
public void success(final JsonObject jsonObject, Response response) {
parseoperators(jsonObject);
Thread t=new Thread(new Runnable() {
@Override
public void run() {
parseoperators(jsonObject);
}
});
t.start();
}
@Override
public void failure(RetrofitError error) {
}
};
public void parseoperators(JsonObject result) {
if(result.has("Status")){
try{
String Status = result.get("Status")
.getAsString();
if (Status.equals("1")) {
Gson gson = new Gson();
JsonArray array = result.get(
"data").getAsJsonArray();
Log.v("TAG_CIRCLELIST", "" + array);
Type type = new TypeToken<ArrayList<Operator>>() {
}.getType();
operator.addAll((Collection<? extends Operator>) gson
.fromJson(array, type));
}
else {
throw new JSONException("Something went wrong ");
}
}
catch(JSONException e){
}
}
}
JSON:
{
"Status": 1,
"data": [
{
"ServiceTypeID": 1,
"OperatorID": 1,
"Operator": "Airtel",
"RechareTypes": [
{
"RechargeTypeID": 1,
"RechargeType": "Topup",
"SMSCode": "AIRT"
}
]
},
{
"ServiceTypeID": 1,
"OperatorID": 2,
"Operator": "BSNL",
"RechareTypes": [
{
"RechargeTypeID": 2,
"RechargeType": "Topup",
"SMSCode": "BSNL"
},
{
"RechargeTypeID": 3,
"RechargeType": "Special",
"SMSCode": "BSNLSPL"
}
]
},
{
"ServiceTypeID": 1,
"OperatorID": 3,
"Operator": "Aircel",
"RechareTypes": [
{
"RechargeTypeID": 4,
"RechargeType": "Topup",
"SMSCode": "AIRC"
}
]
},
{
"ServiceTypeID": 1,
"OperatorID": 4,
"Operator": "Idea",
"RechareTypes": [
{
"RechargeTypeID": 5,
"RechargeType": "Topup",
"SMSCode": "IDEA"
}
]
}
]
}
答案 0 :(得分:1)
Retrofit默认使用Gson将HTTP主体转换为JSON
按如下方式创建运算符pojo
public class Operators {
@Expose
private String Status;
@Expose
private List<Operator> data = new ArrayList<Operator>();
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
public List<Operator> getData() {
return data;
}
public void setData(List<Operator> data) {
this.data = data;
}
}
class Operator {
@Expose
private String ServiceTypeID;
@Expose
private String OperatorID;
@Expose
private String Operator;
@Expose
private List<RechareType> RechareTypes = new ArrayList<RechareType>();
public String getServiceTypeID() {
return ServiceTypeID;
}
public void setServiceTypeID(String ServiceTypeID) {
this.ServiceTypeID = ServiceTypeID;
}
public String getOperatorID() {
return OperatorID;
}
public void setOperatorID(String OperatorID) {
this.OperatorID = OperatorID;
}
public String getOperator() {
return Operator;
}
public void setOperator(String Operator) {
this.Operator = Operator;
}
public List<RechareType> getRechareTypes() {
return RechareTypes;
}
public void setRechareTypes(List<RechareType> RechareTypes) {
this.RechareTypes = RechareTypes;
}
}
class RechareType {
@Expose
private String RechargeTypeID;
@Expose
private String RechargeType;
@Expose
private String SMSCode;
public String getRechargeTypeID() {
return RechargeTypeID;
}
public void setRechargeTypeID(String RechargeTypeID) {
this.RechargeTypeID = RechargeTypeID;
}
public String getRechargeType() {
return RechargeType;
}
public void setRechargeType(String RechargeType) {
this.RechargeType = RechargeType;
}
public String getSMSCode() {
return SMSCode;
}
public void setSMSCode(String SMSCode) {
this.SMSCode = SMSCode;
}
}
更新&#34; void executeCircleAndOperatorsList()&#34;到
void executeCircleAndOperatorsList() {
db.ClearTables();
ServiceClient serviceClient = ServiceUtil.getServiceClient();
Callback<Operators> callback = new Callback<Operators>() {
@Override
public void failure(RetrofitError arg0) {
}
@Override
public void success(Operators operatorList, Response arg1) {
if(operatorList.getStatus().equalsIgnoreCase("1")){
Log.d("Operator_ID", operatorList.getData().get(0).getOperatorID());
}
}
};
serviceClient.getOperators("getRecharge_Operator_v2", callback);
}
相应地更新getoperators方法签名以添加回调Callback<Operators> cb
void getOperators(Callback<Operators> cb);
答案 1 :(得分:0)
使用Retrofit的最佳做法
为Retrofit和GSON编译此库
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
所有网络服务的RequestInterface(POST / GET)
public interface RequestInterface {
@POST("/api/postUserdata")
Call<Boolean> addUserData(@Body User user);
@GET("/api/getAllUsers")
Call<List<Data>> getAllUsersDetails();
@POST("/api/isEmailExist")
Call<Boolean> isEmailExist(@Path("Email") String Email);
}
Webservice客户端API的CommonHelper类
public class CommonHelper {
public RequestInterface service;
public String BASE_URL = "your base url";
public RequestInterface getRetrofitInstance() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(RequestInterface.class);
return service;
}
public boolean isConnectingToInternet(Context _context) {
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
}
在活动中调用Web服务
public void getDataFromServer() {
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.setTitle("Loading");
mProgressDialog.setMessage("Getting data...");
CommonHelper commonHeloper = new CommonHelper();
Call<List<Data>> call= commonHelper.getRetrofitInstance().getAllUsersDetails();
call.enqueue(new Callback<List<Data>>() {
@Override
public void onResponse(Response<List<Data>> response, Retrofit retrofit) {
mProgressDialog.dismiss();
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
mProgressDialog.dismiss();
}
});
}