这是我用于发送和接收带密钥
的字符串值的Web服务Urls.class
public class Urls {
public static final String MAIN_URL="example.com";
}
API.class
public interface API {
@POST("user.php")
Call<MainResponse> registerUser(@Body User user);
@POST("user.php")
Call<MainResponse>loginUser(@Body User user);
@POST("contact.php")
Call<MainResponse>checkNumber(@Body Phone phone);
}
WebService.class
public class WebService {
private static WebService instance;
private API api;
public WebService() {
OkHttpClient client = new OkHttpClient.Builder().build();
Retrofit retrofit = new Retrofit.Builder().client(client)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Urls.MAIN_URL)
.build();
api = retrofit.create(API.class);
}
public static WebService getInstance() {
if (instance == null) {
instance = new WebService();
}
return instance;
}
public API getApi() {
return api;
}
}
MainResponse.class
public class MainResponse {
@SerializedName("status")
public int status;
@SerializedName("message")
public String message;
}
Phone.class
public class Phone {
@SerializedName("phone")
public String phone;
}
MainActivity.class
Phone phone=new Phone();
phone.phone=contactsString[0];
WebService.getInstance().getApi().checkNumber(phone).enqueue(new Callback<MainResponse>() {
@Override
public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
if (response.body().status==1){
//do something
}
}
@Override
public void onFailure(Call<MainResponse> call, Throwable t) {
}
});
我的问题是如何编辑它以发送一个填充了值contactsString[]
的数组并接收另一个数组
答案 0 :(得分:2)
您的服务可以获得单个请求并返回单个响应,如果您是服务的后端开发人员,则应更改服务器端服务,以获取请求列表并返回结果列表< / p>
这是您当前的服务:
@POST("contact.php")
Call<MainResponse>checkNumber(@Body Phone phone);
服务器端开发人员应该为您更改服务,以便能够为电话发送一个这样的对象:
public class Phones {
@SerializedName("phones")
public List<String> phones;
}
在您的回复中,您应该获得请求电话的状态和消息列表
像这样回应:public class MainResponse {
public List<PhoneStatusResponse> phonesStatusList;
}
public class PhoneStatusResponse {
@SerializedName("status")
public int status;
@SerializedName("message")
public String message;
@SerializedName("phoneRequest")
public String phone;
}