Подскажите как отправить json, сделал через Retrofi 2 не чего не улетает
Использовал Retrofi 2
интерфейс ретрофита:
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface RetrofitInterface {
@POST("/bonus/index.php")
Call<JSONResult> getStringScalar(@Body JSONData body);
}
POJO класс 1
public class JSONData {
private String type;
private String phone;
public JSONData(String type, String phoneNum) {
this.type = type;
this.phone = phone;
}
/
*
* @return
* The Phone
*/
public String getPhone() {
return phone;
}
/
*
* @param phone
* The Phone
*/
public void setPhone(String phone) {
this.phone = phone;
}
/
*
* @return
* The Type
*/
public String getType() {
return type;
}
/
*
* @param type
* The Type
*/
public void setType(String type) {
this.type = type;
}
}
POJO класс 2
public class JSONResult {
private Boolean error;
private String message;
private String name;
private String cart;
private String phone;
private String email;
private String level;
private String accumulates;
private String total_balls;
private String available_balls;
private String will_be_credited;
/
*
* @return
* The error
*/
public Boolean getError() {
return error;
}
/
*
* @param error
* The error
*/
public void setError(Boolean error) {
this.error = error;
}
/
*
* @return
* The message
*/
public String getMessage() {
return message;
}
/
*
* @param message
* The message
*/
public void setMessage(String message) {
this.message = message;
}
/
*
* @return
* The name
*/
public String getName() {
return name;
}
/
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/
*
* @return
* The cart
*/
public String getCart() {
return cart;
}
/
*
* @param cart
* The cart
*/
public void setCart(String cart) {
this.cart = cart;
}
/
*
* @return
* The phone
*/
public String getPhone() {
return phone;
}
/
*
* @param phone
* The phone
*/
public void setPhone(String phone) {
this.phone = phone;
}
/
*
* @return
* The email
*/
public String getEmail() {
return email;
}
/
*
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}
/
*
* @return
* The level
*/
public String getLevel() {
return level;
}
/
*
* @param level
* The level
*/
public void setLevel(String level) {
this.level = level;
}
/
*
* @return
* The accumulates
*/
public String getAccumulates() {
return accumulates;
}
/
*
* @param accumulates
* The accumulates
*/
public void setAccumulates(String accumulates) {
this.accumulates = accumulates;
}
/
*
* @return
* The total_balls
*/
public String getTotal_balls() {
return total_balls;
}
/
*
* @param total_balls
* The total_balls
*/
public void setTotal_balls(String total_balls) {
this.total_balls = total_balls;
}
/
*
* @return
* The available_balls
*/
public String getAvailable_balls() {
return available_balls;
}
/
*
* @param available_balls
* The available_balls
*/
public void setAvailable_balls(String available_balls) {
this.available_balls = available_balls;
}
/
*
* @return
* The will_be_credited
*/
public String getWill_be_credited() {
return will_be_credited;
}
/
*
* @param will_be_credited
* The will_be_credited
*/
public void setWill_be_credited(String will_be_credited) {
this.will_be_credited = will_be_credited;
}
}
Вызов в OnClick кнопки:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("Адрес сайта")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitInterface service = retrofit.create(RetrofitInterface.class);
Call<JSONResult> call=service.getStringScalar(new JSONData("type",phoneNum));
call.enqueue(new Callback<JSONResult>() {
@Override
public void onResponse(Call<JSONResult> call, Response<JSONResult> response) {
if(response.body().getError()){
Toast.makeText(getBaseContext(),response.body().getMessage(),Toast.LENGTH_SHORT).show();
}else {
String msg = response.body().getMessage();
boolean error = response.body().getError();
result_text.setText(msg);
}
}