[英]How to send JSON OBJECT as a parameter in Retrofit 2 (Android)
[英]android send several parameter with retrofit
我想將幾個參數發送給服務器進行改造,但是每次這樣做都會出錯。
錯誤標題: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;
}
}
更改您的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);
?>
對不起,我犯了一個錯誤,我向服務器發送了一個null變量,服務器不知道,所以返回一個錯誤,謝謝大家
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.