问题
My service just use post method.
in my code :
my ApiInterFace Class
public interface ApiInterface {
@POST("srrvk")
@FormUrlEncoded
Call<JoinAllAllowedInnoResponse> GETJOINALLOWEDINNOLIST(@Field("a") Integer lastOrderId, @Field("b") Integer rowCount,@Header("wkey") String wkey);
}
and my getArrayList method is like
public void getJoinlist(Integer lastOrderId,Integer rowCount){
showProgressDialog("Please Wait...");
final String wkey = tinyDB.getString(Constant.wkey);
Log.d(CLASS_NAME, "wkey :" +wkey);
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<JoinAllAllowedInnoResponse> call = apiService.GETJOINALLOWEDINNOLIST(lastOrderId,rowCount,wkey);
call.enqueue(new Callback<JoinAllAllowedInnoResponse>() {
@Override
public void onResponse(Call<JoinAllAllowedInnoResponse> call, Response<JoinAllAllowedInnoResponse> response) {
hideProgressDialog();
if (response.body() != null){
if(response.body().getEx() == null){
List<JoinAllAllowedInno> mJoinList = response.body().getJoinList();
mJoinAdapter.setJoinList(mJoinList);
if(mJoinList.size() == 0){
emptyMessage.setVisibility(View.VISIBLE);
}
}else {
getAlert(response.body().getEx());
}
}
}
@Override
public void onFailure(Call<JoinAllAllowedInnoResponse> call, Throwable t) {
hideProgressDialog();
Log.e(CLASS_NAME, t.toString());
}
});
}
my question is when I put a breakpoint at response : noting happened ?
Where is the problem ? Any advice or sample code ?
and my son is :
My json uses Dynamic header
"ex": null,
"c": [
{
"INNOID": 8,
"SHORTDESC": "***************",
"ORDERID": 1,
"JOINEND": 1519074000000,
"LONGDESC": "******************",
"JOINSTART": 1514754000000,
"DOCLINK": "*****************************",
"TEASERVIDEO": "*****"
},
{
"INNOID": 7,
"SHORTDESC": "***********",
"ORDERID": 2,
"JOINEND": **********,
"LONGDESC": "*****************",
"JOINSTART": 1514790000000,
"DOCLINK": "***********************",
"TEASERVIDEO": "*******************"
}
]
}
回答1:
First of all, enter your web service: public interface ApiInterface {
@POST("webservice/whatever")
Another alternative is to give it objects to work with:
Call<JsonElement> GETJOINALLOWEDINNOLIST(@Body YourRequestObject eg);
Then create 2 model classes. One for the request and the other for response. Use the same names as that in the JSON or alternative is to use a Jackson converter: http://www.jsonschema2pojo.org/
You'll figure it out there because I don't know what your key names are. It will give you the java classes so just download them, put them in your model class folder. Then remove all the unnecessary serializable stuff and keep only the names and getters and setters.
Next, create your Retrofit API:
public class RetrofitClassExample{
private static Retrofit retrofit;
final private static String BASE_URL = "https://website.com/";
public static Retrofit getClient(){
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Then finally when calling,
ClassWhereYourCallIS classWhereYourCallIS= RetrofitClassExample.getClient().create(ClassWhereYourCallIS.class);
final Call<JsonElement> yourRequestObjectResponse = classWhereYourCallIS.GETJOINALLOWEDINNOLIST(yourRequestObject);
yourRequestObjectResponse.GETJOINALLOWEDINNOLIST(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
if (response.isSuccessful()) {
String jsonString = response.body().getAsJsonObject().toString().trim();
JSONObject reader = new JSONObject(jsonString );
YourRequestObjectResponse yourRequestObjectResponse = new YourRequestObjectResponse();
JSONObject rp = reader.getJSONObject("ex");
yourRequestObjectResponse.setSomeValue(rp.getString("keyName"));
}
Not sure if this will help but here you go
来源:https://stackoverflow.com/questions/48685656/how-to-post-request-and-get-response-with-retrofit-2