2017-06-16 62 views
1

這是用於發送和接收與鍵改造單值/陣列

一個字符串值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); 

} 

我的web服務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[]的數組,並收到另一個陣列

回答

1

你的服務是一種方式來獲得單一的請求,並給你回單的響應,你應該如果更改服務器端的服務,您是服務的後端開發人員,得到的請求列表,並給回結果

的列表,這是您當前的服務:

@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; 

} 
+0

這是您要發送一個數組什麼,我要求 – RealDEV

+0

的值作爲請求並得到結果。我對嗎? – Meikiem

+0

是的,這就是我要求 – RealDEV