Android 如何通过改装2发布请求并获得响应?
我的服务只是使用post方法 在我的代码中: 我的API接口类Android 如何通过改装2发布请求并获得响应?,android,json,post,retrofit,retrofit2,Android,Json,Post,Retrofit,Retrofit2,我的服务只是使用post方法 在我的代码中: 我的API接口类 public interface ApiInterface { @POST("srrvk") @FormUrlEncoded Call<JoinAllAllowedInnoResponse> GETJOINALLOWEDINNOLIST(@Field("a") Integer lastOrderId, @Field("b") Integer rowCount,@Header("wkey") St
public interface ApiInterface {
@POST("srrvk")
@FormUrlEncoded
Call<JoinAllAllowedInnoResponse> GETJOINALLOWEDINNOLIST(@Field("a") Integer lastOrderId, @Field("b") Integer rowCount,@Header("wkey") String wkey);
}
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());
}
});
}
首先,输入您的web服务: 公共接口{
@POST("webservice/whatever")
另一种选择是为it提供要使用的对象:
Call<JsonElement> GETJOINALLOWEDINNOLIST(@Body YourRequestObject eg);
然后最后打电话的时候,
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"));
}
ClassWhereYourCallIS ClassWhereYourCallIS=classexample.getClient().create(ClassWhereYourCallIS.class);
最终调用yourRequestObjectResponse=classWhereYourCallIS.GETJOINALLOWEDINNOLIST(yourRequestObject);
yourRequestObjectResponse.GETJOINALLOWEDINNOLIST(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
字符串jsonString=response.body().getAsJsonObject().toString().trim();
JSONObject reader=新的JSONObject(jsonString);
YourRequestObjectResponse YourRequestObjectResponse=新建YourRequestObjectResponse();
JSONObject rp=reader.getJSONObject(“ex”);
yourRequestObjectResponse.setSomeValue(rp.getString(“keyName”);
}
不确定这是否有用,但给你
查看此答案,发布json以了解更多信息reference@VishvaDave我更新了我的问题您的json无效检查我检查了它的有效json@VishvaDave
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;
}
}
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"));
}