I am facing a problem,first of all I am new to android, and I am having a Post request, I am sending an object in body :
public class SingleContractRequest {
@SerializedName("userRole")
private String userRole;
@SerializedName("contratId")
private String contratId;
public SingleContractRequest(String userRole, String contractId) {
this.userRole = userRole;
this.contratId = contractId;
}
public String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
this.userRole = userRole;
}
public String getContractId() {
return contratId;
}
public void setContractId(String contractId) {
this.contratId = contractId;
}
}
And that is my ContractApi, the method called is the second one :
public interface ContractApi {
@GET("contrats.php")
Single<List<ContractModel>> getContractList();
@POST("contrat.php")
Single<ContractModel> getContract(@Body SingleContractRequest body);
}
And here is my Module :
@Module
public class ApiModule {
public static String BASE_URL = "http://192.168.1.104/newconceptsphp/";
@Provides
public ContractApi provideContractApi(){
Gson gson = new GsonBuilder()
.setLenient()
.create();
return new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(ContractApi.class);
}
@Provides
public ContractService provideContractService(){
return ContractService.getInstance();
}
}
And to call the api I have a method in my service :
public Single<ContractModel> getContract(SingleContractRequest request) {
return api.getContract(request);
}
So I could do that in one single method but I am creating many layers for f better architecture.
The error I am getting now and I don't know how to solve it :
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
This is the script I am consuming :
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description");
json_decode(file_get_contents("php://input"), true);
$dsn = "mysql:host=localhost;dbname=leranconcepts";
$user = "root";
$passwd = "";
$pdo = new PDO($dsn, $user, $passwd);
if (isset($_POST["userRole"])){
$userRole = $_POST["userRole"];
} else {
$userRole = null;
}
if (isset($_POST["contratId"])){
$contratId = $_POST["contratId"];
} else {
$contratId = null;
}
// managing products
if ($userRole === "VENDEUR"){
$sth = $pdo->prepare("SELECT * FROM contrat WHERE id=?");
} else if($userRole === "COURTIER"){
$sth = $pdo->prepare("SELECT id, imageUrl, courtier FROM contrat WHERE id=?");
}
$sth->execute([$contratId]);
$result = $sth->fetchAll(PDO::FETCH_OBJ);
echo json_encode($result[0]);
?>
I think this is to understand my problem, how to solve it.
Any help would be much appreciated guys.