Java 改造单值/阵列
这是我的web服务,用于发送和接收带有键的字符串值 url.classJava 改造单值/阵列,java,android,retrofit,Java,Android,Retrofit,这是我的web服务,用于发送和接收带有键的字符串值 url.class public class Urls { public static final String MAIN_URL="example.com"; } public interface API { @POST("user.php") Call<MainResponse> registerUser(@Body User user); @POST("user.php") Cal
public class Urls {
public static final String MAIN_URL="example.com";
}
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);
}
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;
}
}
public class MainResponse {
@SerializedName("status")
public int status;
@SerializedName("message")
public String message;
}
public class Phone {
@SerializedName("phone")
public String phone;
}
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) {
}
});
API.class
public class Urls {
public static final String MAIN_URL="example.com";
}
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);
}
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;
}
}
public class MainResponse {
@SerializedName("status")
public int status;
@SerializedName("message")
public String message;
}
public class Phone {
@SerializedName("phone")
public String phone;
}
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) {
}
});
MainResponse.class
public class Urls {
public static final String MAIN_URL="example.com";
}
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);
}
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;
}
}
public class MainResponse {
@SerializedName("status")
public int status;
@SerializedName("message")
public String message;
}
public class Phone {
@SerializedName("phone")
public String phone;
}
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) {
}
});
Phone.class
public class Urls {
public static final String MAIN_URL="example.com";
}
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);
}
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;
}
}
public class MainResponse {
@SerializedName("status")
public int status;
@SerializedName("message")
public String message;
}
public class Phone {
@SerializedName("phone")
public String phone;
}
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) {
}
});
MainActivity.class
public class Urls {
public static final String MAIN_URL="example.com";
}
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);
}
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;
}
}
public class MainResponse {
@SerializedName("status")
public int status;
@SerializedName("message")
public String message;
}
public class Phone {
@SerializedName("phone")
public String phone;
}
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) {
}
});
Phone Phone=新手机();
phone.phone=联系人字符串[0];
WebService.getInstance().getApi().checkNumber(phone).enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.body().status==1){
//做点什么
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
我的问题是如何编辑它以发送一个充满值的数组
contactsString[]
并接收另一个数组您的服务可以获取单个请求并返回单个响应,如果您是该服务的后端开发人员,您应该更改服务器端服务,获取请求列表并返回结果列表
这是您当前的服务:
@POST("contact.php")
Call<MainResponse>checkNumber(@Body Phone phone);
@POST(“contact.php”)
CallcheckNumber(@Body 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;
}
公共类main响应{
公共列表电话状态列表;
}
公共类PhoneStatusResponse{
@序列化名称(“状态”)
公众地位;
@SerializedName(“消息”)
公共字符串消息;
@SerializedName(“电话请求”)
公用串电话;
}
这就是我所要求的。如果您希望发送一个值数组作为请求并获得结果。我说得对吗?我需要知道在这段代码中要编辑什么才能将其更改为发送数组和接收数组,所以我要告诉你。由于对服务器的改装请求是异步任务,并且结果中没有足够的信息来识别哪个请求是异步任务,因此您不能期望ask更改服务—您所说的都是关于客户端编码的,您应该从服务器端更改服务