【发布时间】:2017-07-05 08:40:34
【问题描述】:
我试图使用改造发送 JSONObject 请求(POST 方法)并将结果作为 JSONObject,我该如何实现。?我的内容类型是(应用程序/json) 你能分享一些你最好的方法吗?
改造接口类
public interface RequestClient {
@Headers("Content-Type: application/json")
@POST("TripmakersLowFareShopping")
Call<FlightResponse>pushRequest(@Body FlightRequest request); }
请求模型类
public class FlightRequest {
@SerializedName("AgencyID")
@Expose
private String agencyID;
@SerializedName("BranchID")
@Expose
private String branchID;
@SerializedName("OriginDestinationInformations")
@Expose
private ArrayList<OriginDestinationInformation> originDestinationInformations = null;
@SerializedName("PassengerTypeQuantities")
@Expose
private ArrayList<PassengerTypeQuantity> passengerTypeQuantities = null;
@SerializedName("Password")
@Expose
private String password;
@SerializedName("PricingSourceType")
@Expose
private Integer pricingSourceType;
@SerializedName("RequestOptions")
@Expose
private Integer requestOptions;
@SerializedName("SessionId")
@Expose
private Object sessionId;
@SerializedName("SupplierParameterList")
@Expose
private ArrayList<SupplierParameterList> supplierParameterList = null;
@SerializedName("Target")
@Expose
private Integer target;
@SerializedName("TravelPreferences")
@Expose
private TravelPreferences travelPreferences;
@SerializedName("UserName")
@Expose
private String userName;
/**
* No args constructor for use in serialization
*
*/
public FlightRequest() {
super();
this.agencyID = "20083";
this.branchID = "10161";
this.password = "Tj9%2f30XvS59GOBsjHuMd%2bg%3d%3d";
this.pricingSourceType = 0;
this.requestOptions = 0;
this.sessionId = null;
this.target = 0;
this.userName = "qc@*****.com";
}
public String getAgencyID() {
return agencyID;
}
public void setAgencyID(String agencyID) {
this.agencyID = agencyID;
}
public String getBranchID() {
return branchID;
}
public void setBranchID(String branchID) {
this.branchID = branchID;
}
public List<OriginDestinationInformation> getOriginDestinationInformations() {
return originDestinationInformations;
}
public void setOriginDestinationInformations(ArrayList<OriginDestinationInformation> originDestinationInformations) {
this.originDestinationInformations = originDestinationInformations;
}
public List<PassengerTypeQuantity> getPassengerTypeQuantities() {
return passengerTypeQuantities;
}
public void setPassengerTypeQuantities(ArrayList<PassengerTypeQuantity> passengerTypeQuantities) {
this.passengerTypeQuantities = passengerTypeQuantities;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getPricingSourceType() {
return pricingSourceType;
}
public void setPricingSourceType(Integer pricingSourceType) {
this.pricingSourceType = pricingSourceType;
}
public Integer getRequestOptions() {
return requestOptions;
}
public void setRequestOptions(Integer requestOptions) {
this.requestOptions = requestOptions;
}
public Object getSessionId() {
return sessionId;
}
public void setSessionId(Object sessionId) {
this.sessionId = sessionId;
}
public List<SupplierParameterList> getSupplierParameterList() {
return supplierParameterList;
}
public void setSupplierParameterList(ArrayList<SupplierParameterList> supplierParameterList) {
this.supplierParameterList = supplierParameterList;
}
public Integer getTarget() {
return target;
}
public void setTarget(Integer target) {
this.target = target;
}
public TravelPreferences getTravelPreferences() {
return travelPreferences;
}
public void setTravelPreferences(TravelPreferences travelPreferences) {
this.travelPreferences = travelPreferences;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
} 响应模型类
public class FlightResponse {
private JSONObject responceData;
public JSONObject getResponceData() {
return responceData;
}
public void setResponceData(JSONObject responceData) {
this.responceData = responceData;
}
}
改造声明和功能部分
rqbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<OriginDestinationInformation> originlist = new ArrayList<>();
originlist.add(new OriginDestinationInformation(null,"/Date(1503599400000)/",null,"COK","DXB"));
ArrayList<PassengerTypeQuantity> typeQuantities = new ArrayList<>();
ArrayList<SupplierParameterList>supplierParameterLists = new ArrayList<SupplierParameterList>();
supplierParameterLists.add(new SupplierParameterList(false,"EndUserIPAddress","61.0.250.97"));
supplierParameterLists.add(new SupplierParameterList(false,"EndUserBrowserAgent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"));
supplierParameterLists.add(new SupplierParameterList(false,null,null));
supplierParameterLists.add(new SupplierParameterList(false,"UserData","qc@tripmakers.com,971_527564545,Fateh"));
supplierParameterLists.add(new SupplierParameterList(false,"RequestOrigin","US-localhost"));
typeQuantities.add(new PassengerTypeQuantity(1,1));
TravelPreferences travelPreferences = new TravelPreferences(1,1);
FlightRequest request = new FlightRequest();
request.setOriginDestinationInformations(originlist);
request.setPassengerTypeQuantities(typeQuantities);
request.setTravelPreferences(travelPreferences);
request.setSupplierParameterList(supplierParameterLists);
requestCall(request);
}
});
}
private void requestCall(FlightRequest request) {
OkHttpClient.Builder httpClient ;
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://192.168.1.125:8013/flight/").addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
RequestClient client = retrofit.create(RequestClient.class);
Call<FlightResponse> requestCall = client.pushRequest(request);
requestCall.enqueue(new Callback<FlightResponse>() {
@Override
public void onResponse(Call<FlightResponse> call, Response<FlightResponse> response) {
Toast.makeText(RetrofitResults.this, "Sucess", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<FlightResponse> call, Throwable t) {
Toast.makeText(RetrofitResults.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
}
【问题讨论】:
-
你搜索过类似的查询吗?你有没有尝试过什么。 ?我们不是来为您编写代码的,如果您有问题,我们会在这里为您提供支持。
-
对不起先生,这是我在堆栈中的第一个问题,我已经添加了我已经完成的代码 sn-p,请纠正我@MatM
标签: android json post android-volley retrofit2