首页 > 解决方案 > 无法使用 Retrofit 通过 POST 发送数据?

问题描述

我面临一个问题,首先我是 android 新手,我有一个 Post 请求,我在 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;
    }
}

这就是我的 ContractApi,调用的方法是第二个:

public interface ContractApi {

    @GET("contrats.php")
    Single<List<ContractModel>> getContractList();

    @POST("contrat.php")
    Single<ContractModel> getContract(@Body SingleContractRequest body);

}

这是我的模块:

@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();
    }

}

要调用 api,我的服务中有一个方法:

public Single<ContractModel> getContract(SingleContractRequest request) {
     return api.getContract(request);
}

所以我可以用一种方法做到这一点,但我正在创建许多层以获得更好的架构。

我现在遇到的错误,我不知道如何解决它:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_OBJECT 但在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224) 的第 1 行第 1 列路径 $ 为字符串retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)

这是我正在使用的脚本:

<?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]);


?>

我想这是为了了解我的问题,如何解决它。

任何帮助将不胜感激。

标签: androidapihttp-postretrofit

解决方案


推荐阅读