我想通过改造向服务器发送几个参数,但每次我这样做都会出错。
错误标题: com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(true)接受第1行第1行路径中格式错误的JSON $
我不知道出了什么问题。 这是我的代码
java代码:
@FormUrlEncoded
@POST("rating.php")
Call<Message> addComment(@Field("app_id") String appId, @Field("user_id") String userId,@Field("comment_title") String comment,@Field("star") int star);
php代码:
$message=array();
$appId=$_POST["app_id"];
$userId=$_POST["user_id"];
$title=$_POST["comment_title"];
$star=$_POST["star"];
$message["message"]="ok";
echo json_encode($message);
我的Message.java:
public class Message {
@SerializedName("message")
public String message;
public String getMessage() {
return message;
}
public void setMessage(String title) {
this.message = title;
}
}
答案 0 :(得分:0)
更改PHP脚本以获得正确的JSON响应
[{"message":"ok"}]
<?php
$appId=$_POST["app_id"];
$userId=$_POST["user_id"];
$title=$_POST["comment_title"];
$star=$_POST["star"];
$message["message"]="ok";
header('Content-Type: application/json; Accept-Charset: utf-8; ');
$response = array();
array_push($response, $message);
echo json_encode($response);
?>
答案 1 :(得分:0)
对不起,我犯了一个错误,我发送一个null变量到服务器,服务器不知道所以返回给我一个错误,谢谢你们