RealDEV
RealDEV

Reputation: 131

Retrofit single value / array

this is my web service for sending and receiving a string value with key

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) {

                }
            });

My question is how to edit this to send an array filled with values contactsString[] and receive another array

Upvotes: 1

Views: 835

Answers (1)

Meikiem
Meikiem

Reputation: 1936

Your service is in a way to get single request and give you back single response, you should change server side service if you are the backend developer of the service, to get a list of request and give back a list of result

this is your current service:

  @POST("contact.php")
Call<MainResponse>checkNumber(@Body Phone phone);

server side developer should change service for you to be able to send in body an object like this for Phones:

public class Phones {

@SerializedName("phones")
public List<String> phones;
}

and in your response you should get list of status and messages with request phones

response like this:

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;

}

Upvotes: 2

Related Questions