I have seen two or more tutorial to apply post using retrofit 2.0. All they implemented the same as i did. But i get the error like below
onFailure: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
I want to pass simple string that i have take from edit text. then post it in my server DB... But it just returns this exception. How to make it right I want to recieve data as string on server side or i want to send the data as string
Api Service interface
import retrofit.Call;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.POST;
/**
* Created by Shaon on 8/14/2016.
*/
public interface APIService {
@GET("my_json")
Call<List<People>> getPeopleDetails();
@POST("my_json/insert.php")
Call<People> setPeopleDetails(@Body People people);
}
People class
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Shaon on 8/14/2016.
*/
public class People {
private String id = "";
@SerializedName("name")
@Expose
private String name = "";
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My post method
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://w...content-available-to-author-only...e.org/").
addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
People people = new People();
people.setName(editName.getText().toString());
Call<People> peopleCall = service.setPeopleDetails(people);
peopleCall.enqueue(new Callback<People>() {
@Override
public void onResponse(Response<People> response, Retrofit retrofit) {
hidepDialog();
Log.d("onResponse", "There is an error");
}
@Override
public void onFailure(Throwable t) {
hidepDialog();
Log.d("onFailure", t.toString());
}
});
My server side code
$id = $_POST["id"];
$name = $_POST["name"];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO mytable (name)
VALUES ('$name')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
" . $e->getMessage(); } $conn = null; ?> – Shaon Aug 15 '16 at 11:15