android - 无法使用 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]);
?>
我想这是为了了解我的问题,如何解决它。
任何帮助将不胜感激。
解决方案
推荐阅读
- html - 如何使用node.js将数据从html发送到three.js?
- google-sheets - 在电子表格中创建评论时,Google Drive API 会忽略锚参数
- artifactory - 自动部署到 Artifactory Distribution Repo
- python - 从 Python 中的 `file` 确定 `file.read()` 的结果类型
- git - Git - 将未提交的更改保存到不同的分支
- node.js - 从 Lambda 创建 Elasticsearch 映射时出现“授权异常”
- javascript - 尝试在 MSSQL 中使用 FOR JSON 并尝试读取根键时接收未定义
- youtube - 将 youtube 视频作为全息图播放
- javascript - Power BI - 从发布请求中获取数据
- c++ - C++ 标准是否保证插入关联容器失败不会修改右值引用参数?