0

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.

TaouBen
  • 1,165
  • 1
  • 15
  • 41
  • 1
    it would help to look at the actual content of the response that you get from your server. – njzk2 Apr 12 '20 at 19:47
  • I am calling a php script, I have to send something like this { userRole: "SOMETHING", contratId: "1" } and I get a response liek this : { username: "SOMETHING", data: "data.." } – TaouBen Apr 12 '20 at 19:51
  • Do you need more than this ? I can share the script if you are familiar with php – TaouBen Apr 12 '20 at 19:52
  • I edited my answer, I provided the php script – TaouBen Apr 12 '20 at 19:57
  • 1
    please try using .client(httpClient.build()) on Retrofit.Builder – Prashant.J Apr 12 '20 at 20:03
  • Prashant : can you write a code, because httpClient is variable that I have not declared, I don't know about this OkHttpClient – TaouBen Apr 12 '20 at 20:05
  • @njzk2 anything to help with Sir ? – TaouBen Apr 12 '20 at 20:09
  • 1
    Use a logger on retrofit (e.g.:https://stackoverflow.com/questions/32514410/logging-with-retrofit-2) to see what exactly the server is returning – njzk2 Apr 12 '20 at 20:14
  • I have used it, and I am getting an error, my variable are ot defined in php, which means I think I am not sending them right, because I already tested the route in Postman – TaouBen Apr 12 '20 at 20:26
  • but it's weird, because I see also I got them 2020-04-12 21:23:54.090 32331-32428/com.example.cosysimulation D/OkHttp: Content-Length: 38 2020-04-12 21:23:54.090 32331-32428/com.example.cosysimulation D/OkHttp: {"contratId":"1","userRole":"VENDEUR"} 2020-04-12 21:23:54.090 32331-32428/com.example.cosysimulation D/OkHttp: --> END POST (38-byte body) – TaouBen Apr 12 '20 at 20:28
  • My problem now is how to transform the post request into php code, thanks for the indication, have a good day njzk2 and Prashat.J – TaouBen Apr 12 '20 at 20:46
  • Found a solution to get a response, thanks – TaouBen Apr 12 '20 at 20:57

0 Answers0