0

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;
?>
Shaon
  • 2,496
  • 26
  • 27
  • What is the JSON response that you expect? Gson is telling you that it is returning just a string, not an Object – OneCricketeer Aug 15 '16 at 10:39
  • @cricket_007 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 – Shaon Aug 15 '16 at 10:42
  • I can see what you want. That's not what I asked. – OneCricketeer Aug 15 '16 at 10:46
  • 1
    Possible duplicate of ["Expected BEGIN\_OBJECT but was STRING at line 1 column 1"](http://stackoverflow.com/questions/28418662/expected-begin-object-but-was-string-at-line-1-column-1) – OneCricketeer Aug 15 '16 at 10:49
  • @cricket_007 I have tried this. But failed to apply it with people class. will you provide me simple code to add the { bracket – Shaon Aug 15 '16 at 10:56
  • Read the duplicate? Your server is not returning a JSON object. You can't use Retrofit with the Gson converter unless you are only getting JSON. Try to go to the address of the URL in your browser, and please copy the string that is returned into your question with an [edit] – OneCricketeer Aug 15 '16 at 10:58
  • I can't give you server side code, I don't even know what language your web server is running. This isn't something you fix in Retrofit. Not yet, at least – OneCricketeer Aug 15 '16 at 10:59
  • @cricket_007 $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 . "
    " . $e->getMessage(); } $conn = null; ?>
    – Shaon Aug 15 '16 at 11:15
  • This is my server code using PDO. it only receives string as i think.. so what should i do to send data as string – Shaon Aug 15 '16 at 11:15
  • `echo "New record created successfully"; ` Does that look like JSON? Nope, it's just a string. Also, please don't post code in the comments. You may [edit] your question – OneCricketeer Aug 15 '16 at 11:20
  • This might help. http://php.net/manual/en/function.json-encode.php – OneCricketeer Aug 15 '16 at 11:30
  • i am new to retrofit so it seems all confusing. I have don my json parsing with the same server code using volley – Shaon Aug 15 '16 at 11:31
  • You are welcome to continue using Volley. Your problem is not Retrofit, it's Gson trying to convert a String into an Object. Volley would have the same problem – OneCricketeer Aug 15 '16 at 11:32
  • BTW thanks. I will try myself now – Shaon Aug 15 '16 at 11:32
  • @cricket_007 Is there any way to send the data using just name as a field so that i can receive it as name field. so i dont need to change my php code – Shaon Aug 15 '16 at 11:34
  • Yes, but not using Retrofit or using any JSON. You just POST `name` as a regular HTTP body parameter. – OneCricketeer Aug 15 '16 at 11:38
  • @cricket_007 So if i want to use retrofit i need to change my server side code also... need to work with new php code... was my whole android code correct.? just need to change my php code!! right? – Shaon Aug 15 '16 at 11:42
  • At a quick glance, I don't see anything wrong with your Retrofit code. The Person Retrofit call is simply expecting a JSON object like `{"name": "Shaon"}` – OneCricketeer Aug 15 '16 at 12:00

0 Answers0